我从这里下载了gtest 1.7.0源代码: https://code.google.com/p/googletest/downloads/list 并在ubuntu 13.10上构建gtest .a文件(lib文件): Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64
https://code.google.com/p/googletest/downloads/list
并在ubuntu 13.10上构建gtest .a文件(lib文件):
Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
并生成lib:libgtest.a.在我的main.cpp文件中有:
#include <iostream> #include "gtest/gtest.h" int main(){ std::cout << "Test \n"; int argc = 2; char* cp01; char* cp02; char* argv[] = {cp01, cp02}; testing::InitGoogleTest(&argc, argv); return 0; }
从我建立的终端:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lpthread -lgtest
这给出了以下错误:
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create' /home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific' /home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete' /home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific' collect2: error: ld returned 1 exit status
基于此:
error during making GTest
我也试过-pthread而不是-lpthread但是给出了同样的错误:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -pthread -lgtest
编辑:我也尝试将-pthread指定为最后一个参数:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread
同样的错误
我究竟做错了什么?
希望链接静态库/home/user/gtest-1.7.0/lib/.libs/libgtest.a.
代替:
g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread
使用:
g++ main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread
请注意,您的命令行不会为生成的可执行文件提供任何名称,这将是默认值
到a.out.如果你想要它,例如mytest,然后做:
g++ -o mytest main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread