小编enz*_*959的帖子

对'pthread_mutex_trylock'的未定义引用

我有以下测试程序.

#include <iostream>
#include <cstdlib>

using namespace std;    
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])
{
  int iret;
  iret = pthread_mutex_trylock( & mymutex );
  cout << "Test2 !!! " << endl;
  pthread_mutex_unlock( & mymutex );
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

如果我在没有添加pthread库的情况下编译它,我会得到pthread_mutex_trylock的未解决错误错误,但仅针对函数pthread_mutex_trylock.

如果我用pthread_mutex_trylock替换pthread_mutex_trylock,程序就会被编译并运行得很好而且没有-lpthread*选项.

如果我在编译命令中添加-lpthraed选项,那么运行良好,运行良好: $ g ++ test2.c -o test2 -lpthread 这个警告未解决: $ g ++ test2.c -o test2

示例错误输出:$ g ++ test2.c -o test2 /tmp/ccU1bBdU.o:在函数main': test2.c:(.text+0x11): undefined reference topthread_mutex_trylock中,collect2:ld返回1退出状态

如果我替换指令iret = pthread_mutex_trylock(&mymutex);

iret = pthread_mutex_lock(&mymutex); 如果没有将pthread libarry添加到编译命令,程序编译并运行也没有错误我知道如果我没有使用-lpthread选项那么有未解决的错误是正确的,但为什么我没有相同的未解决错误还有其他pthread_函数? …

c++ pthreads

13
推荐指数
2
解决办法
3万
查看次数

Valgrind在为字符串赋值时报告内存泄漏

Valgrind在为字符串赋值时报告内存泄漏.

我使用以下简单代码来测试Valgrind报告的内存泄漏.

/******************************************
* FILE: t3.c
* Compiled using : g++ -g t3.c -o t3
*
* $ g++ -v
* Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.6/specs
* Configured with: ./configure --prefix=/usr --infodir=/share/info --mandir=/share/man
*      --enable-languages=c,c++ --with-system-zlib --program-suffix=-3.4 --enable-threads=posix
* Thread model: posix
* gcc version 3.4.6
 ******************************************/


#include <iostream>
#include <string>

using namespace std;

/**************************************************************
 **************************************************************/
int main(int argc, char *argv[])
{
   string test = "XXXXXXXXX";
   cout << "this is a test " << test << endl;
   exit(0);
}
Run Code Online (Sandbox Code Playgroud)

我使用这个命令编译:

$ …
Run Code Online (Sandbox Code Playgroud)

c++ string valgrind memory-leaks g++

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×2

g++ ×1

memory-leaks ×1

pthreads ×1

string ×1

valgrind ×1