【问题标题】:C++ compile with libraries [duplicate]C ++用库编译[重复]
【发布时间】:2014-05-27 11:00:51
【问题描述】:

我在 Linux 中的 c++ 应用程序中工作,基本上我想做的是这个,我有两个 .cpp 文件,其中一个是使用线程的 main.cpp。我的问题是我试图制作这样的可执行文件: g++ -c main.cpp -> I get main.o g++ -c second.cpp -> I get second.o g++ -o executable main.o second.o-> 我得到:2ficheros.cpp:(.text+0xa4): undefined reference to `pthread_create'

所以我想我的错误是因为我必须以相同的方式关联 pthread 库,但我不知道该怎么做。我在网上看,我发现我必须做这样的事情:g++ -o executable main.o second.o pthread.o 但我不知道它是否正确,我看不到 pthread.o 只是 pthread.h。

对不起,如果我在没有知识的情况下说话,请有人可以帮助我。

【问题讨论】:

  • g++ -o executable main.cpp second.cpp -pthread
  • 在上述所有命令中使用-pthread 选项。

标签: c++ linux gcc


【解决方案1】:

您需要链接 pthreads 支持:

c++ main.o second.o -o executable -pthread

是的,没有-l。对于其他库(例如数学库),请使用:

c++ main.o second.o -o executable -pthread -lm

如果您在其他目录中有另一个第三方库:

c++ main.o second.o -o executable -pthread -L/opt/coolmolib -lcoolmolib

注意我没有直接使用“g++”,而是一个符号链接,c++。在某些系统上,这可能是 clang(在 debian apt install clang 上安装 clang)。

【讨论】:

  • 非常感谢完美的 cuco!
猜你喜欢
  • 2017-11-01
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 2017-01-05
相关资源
最近更新 更多