在各种多线程C和C++项目中,我看到-pthread标志应用于编译和链接阶段,而其他人根本不使用它,只是传递-lpthread给链接阶段.
有没有危险没有编译和链接-pthread国旗 - 即-pthread实际做什么?我主要对Linux平台感兴趣.
如果我直接使用g ++在命令行上编译,我可以看到我需要的一切:
$ g++ -pthread test.cpp
$ ldd a.out
linux-vdso.so.1 => (0x00007fffd05b3000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4a1ba8d000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4a1b870000)
...more...
Run Code Online (Sandbox Code Playgroud)
然后我尝试为这个5行测试应用程序创建一个简单的cmake文件:
$ cat CMakeLists.txt
PROJECT ( Test CXX )
CMAKE_MINIMUM_REQUIRED ( VERSION 2.8 )
FIND_PACKAGE ( Threads REQUIRED )
ADD_EXECUTABLE ( test test.cpp )
TARGET_LINK_LIBRARIES ( test ${CMAKE_THREAD_LIBS_INIT} )
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚为什么CMake找不到它需要用的东西Threads:
$ cd build/
$ cmake ..
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:288 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-2.8/Modules/FindThreads.cmake:166 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) …Run Code Online (Sandbox Code Playgroud) 我刚刚拉了一个 git 存储库,我和我的朋友正在其中开发一个应用程序。当我运行make我面临这个错误:
对符号“pthread_create@@GLIBC_2.2.5”/lib/x86_64-linux-gnu/libpthread.so.0 的未定义引用:添加符号时出错:命令行中缺少 DSO collect2:错误:ld 返回 1 退出状态 Makefile:182:recipe对于目标 'bin/release/ns' 失败 make[1]:* [bin/release/ns] 错误 1 Makefile:133:目标 'release' 的配方失败 make:* [release] 错误 2
我的朋友拉了同一个分支,他运行它没有任何问题。
你能给我一些关于修复的提示吗?详细的答案将不胜感激。
我正在使用 Android NDK 和 Cmake 来生成我的项目的共享库。
我正在将现有项目从 Ubuntu 移植到 Android,现在我需要移植一些可执行文件。我成功编译了所有需要Threads库的可执行文件 sexecpt 。
在 CMakeList.txt 中,FIND_PACKAGE(Threads)可以在为 Ubuntu 编译时找到该库,但不适用于 Android。
我跟着这个cmake 和 libpthread但没有成功。
我想我应该写这个FindThread.cmake文件,但我对 CMake 还很陌生,我真的不知道该怎么做,尤其是因为我不知道 Android 的线程库在哪里。
任何帮助,将不胜感激。谢谢
我正在尝试在CLion中运行这个简单的线程c ++程序
#include <iostream>
#include <thread>
using namespace std;
//Start of the thread t1
void hello() {
cout << "Hello,concurrent world!" << endl; }
int main() {
thread t1(hello); // spawn new thread that calls hello()
cout << "Concurrency has started!" << endl;
t1.join();
cout << "Concurrency completed!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是有一个未定义的引用pthread的错误,我没有看出我做错了什么...请注意我在CLion上这样做.
我正在尝试在 Linux (Ubuntu) 操作系统下使用 CLion 编译我的程序。
我的 CMake 文件:
# cmake_minimum_required(VERSION 3.5)
project(untitled2 C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_FLAGS -pthread)
add_executable(untitled2 main.c)
Run Code Online (Sandbox Code Playgroud)
我的程序使用线程,所以我添加 set(CMAKE_CXX_FLAGS -pthread)了编译程序所需的内容。我收到一个编译错误:“对 pthread_create 的未定义引用”
我可以使用以下命令通过终端编译程序:
gcc main.c -o main -pthread
Run Code Online (Sandbox Code Playgroud)
我认为我的问题与 CMake 文件有关。有人可以帮我解决这个问题吗?
谢谢!
我正在尝试在 ubuntu 14.04 下的 Clion 中编写 CMakeList.txt,如下所示:
cmake_minimum_required(VERSION 2.8.3)
project(ify)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")
## System dependencies are found with CMake's conventions
find_package(Boost REQUIRED)
include_directories(
${catkin_INCLUDE_DIRS}
/usr/local/include
/usr/include
include
)
include_directories(
include/
)
link_directories(
/usr/local/lib
/usr/lib
libs/x64
/usr/lib/x86_64-linux-gnu
)
#set(SOURCE_FILES main.cpp)
#add_executable(ify ${SOURCE_FILES})
add_executable(ify main.cpp)
target_link_libraries(ify ${catkin_LIBRARIES}
libmsc.so
libasound.so
)
Run Code Online (Sandbox Code Playgroud)
当我运行时它告诉我:
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlopen'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlclose'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlerror'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlsym'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `pthread_create'
Run Code Online (Sandbox Code Playgroud)
我搜索后尝试了一些方法:
1:像这样添加ldl:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} …Run Code Online (Sandbox Code Playgroud) 如何使用cmake运行boost asio?没有这个简单的项目布局:
\nc1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 c1\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.cpp\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\nRun Code Online (Sandbox Code Playgroud)\nCMakeLists.txt:
cmake_minimum_required(VERSION 3.20.0)\nproject(c1)\n\nadd_executable(c1 c1/main.cpp)\ninclude_directories(.)\n\nset(BOOST_ROOT /usr/local/boost_1_78_0)\nfind_package(Boost 1.78.0 REQUIRED COMPONENTS system)\ntarget_include_directories(c1 PUBLIC ${Boost_INCLUDE_DIR})\ntarget_link_libraries(c1 LINK_PUBLIC ${Boost_LIBRARIES})\nRun Code Online (Sandbox Code Playgroud)\n和main.cpp:
#include <iostream>\n#include <boost/asio.hpp>\n\nint main(){\n boost::asio::io_context io;\n boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));\n t.wait();\n std::cout << "Hello world!" << std::endl;\n}\nRun Code Online (Sandbox Code Playgroud)\n我收到此 cmake 错误:
\nCMake Error at /usr/local/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):\n Could NOT find Boost (missing: system) (found suitable version "1.78.0",\n minimum required is "1.78.0")\nCall Stack (most recent call first):\n /usr/local/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)\n /usr/local/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)\n CMakeLists.txt:8 (find_package)\n …Run Code Online (Sandbox Code Playgroud) 我想使用最新版本的Boost库,并具有以下CMakeLists.txt文件的内容:
cmake_minimum_required (VERSION 3.0)
project (foo)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -DBOOST_ERROR_CODE_HEADER_ONLY -lpthread")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
add_executable (first first.cpp)
Run Code Online (Sandbox Code Playgroud)
有了这个,我不断收到以下链接器错误:
对`pthread_detach的未定义引用
但是,如果我在不使用cmake的情况下编译代码,请使用以下命令:
g++ foo.cpp -std=c++11 -DBOOST_ERROR_CODE_HEADER_ONLY -lpthread
Run Code Online (Sandbox Code Playgroud)
它工作正常.
问题是如何使用cmake使其工作.当我通过设置CMAKE_CXX_FLAGS指定编译器标志时,为什么它不起作用?我想我可能不得不指定CMAKE_EXE_LINKER_FLAGS,但这样做根本没有帮助.
感谢所有提供帮助的人!最终起作用的是改变:
set(CMAKE_CXX_COMPILER "gcc-10")
set(CMAKE_C_COMPILER "g++-10")
Run Code Online (Sandbox Code Playgroud)
到:
set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang")
set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang++")
Run Code Online (Sandbox Code Playgroud)
尝试将 boost 链接到我的程序时出现以下错误:
[ 83%] Linking CXX executable cartogram
Undefined symbols for architecture x86_64:
"__ZN5boost15program_options11to_internalERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
__ZN5boost15program_options11to_internalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESt6vectorIS7_SaIS7_EERKS8_IT_SaISB_EE in main.cpp.o
"__ZN5boost15program_options16validation_error12get_templateB5cxx11ENS1_6kind_tE", referenced from:
__ZN5boost15program_options10validators17get_single_stringIcEERKNSt7__cxx1112basic_stringIT_St11char_traitsIS5_ESaIS5_EEERKSt6vectorIS9_SaIS9_EEb in main.cpp.o
"__ZN5boost15program_options19options_descriptionC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj", referenced from:
_main in main.cpp.o
"__ZN5boost15program_options20invalid_option_valueC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
__ZN5boost15program_options8validateIicEEvRNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIT0_St11char_traitsIS7_ESaIS7_EEESaISB_EEPT_l in main.cpp.o
"__ZN5boost15program_options22error_with_option_nameC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9_S9_i", referenced from:
__ZN5boost15program_options10validators17get_single_stringIcEERKNSt7__cxx1112basic_stringIT_St11char_traitsIS5_ESaIS5_EEERKSt6vectorIS9_SaIS9_EEb in main.cpp.o
"__ZN5boost15program_options3argB5cxx11E", referenced from:
__ZNK5boost15program_options11typed_valueIbcE4nameB5cxx11Ev in main.cpp.o
__ZNK5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcE4nameEv in main.cpp.o
__ZNK5boost15program_options11typed_valueIicE4nameB5cxx11Ev in main.cpp.o
"__ZN5boost15program_options6detail7cmdline21set_additional_parserENS_9function1ISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_ERKSA_EE", referenced from:
__ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_ERKSA_EE in main.cpp.o
"__ZN5boost15program_options6detail7cmdlineC2ERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EE", referenced from:
__ZN5boost15program_options25basic_command_line_parserIcEC1EiPKPKc in main.cpp.o
"__ZN5boost15program_options8validateERNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EEPS9_i", referenced …Run Code Online (Sandbox Code Playgroud) 第一次运行命令时出现以下错误
cmake .
Run Code Online (Sandbox Code Playgroud)
但是如果我再次运行该命令,则编译成功。也就是说,我必须运行cmake 两次才能编译项目。为什么会这样?我怎么能修好呢?
错误信息:
CMakeFiles/exec.dir/Timer.cpp.o: In function `std::thread::thread<Timer::Timer()::{lambda()#1}>(Timer::Timer()::{lambda()#1}&&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/exe.dir/build.make:146: recipe for target 'exe' failed
make[2]: *** [exe] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/exe.dir/all' failed
make[1]: *** [CMakeFiles/exe.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -g -std=c++14 -std=c++11 -std=c++17 ")
project(myproject)
set(SOURCE_FILES main.cpp Timer.cpp TimerEntity.cpp)
add_executable(exe ${SOURCE_FILES})
Run Code Online (Sandbox Code Playgroud) 我想基于 .cpp、.h 和多个 .so 文件重建一个简单的应用程序。据我所见,我的 CMakeLists.txt 应该是这样的:
\ncmake_minimum_required(VERSION 3.5) \nset(CMAKE_CXX_STANDARD 11) \nproject(test C CXX)\n\nadd_executable(${PROJECT_NAME} main.cpp)\n\ntarget_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib) \ntarget_link_libraries(test ${CMAKE_SOURCE_DIR}/libA.so ${CMAKE_SOURCE_DIR}/libB.so) \nRun Code Online (Sandbox Code Playgroud)\n所有文件都在同一个文件夹中。我之前将 .cpp 与 .h 文件正确链接。cmake .没有给我任何错误,但使用后make我得到:
main.cpp:(.text+0xf2d) : undefined reference to \xc2\xab pthread_create \xc2\xbb\nRun Code Online (Sandbox Code Playgroud)\n这是一个不属于我的 .h 文件的函数,因此它应该位于 .so 文件中。我不知道问题是来自链接还是文件 .so 本身。
\n我还有同名的文件,例如 libA.so、libA.so.0 或 libA.so.0.2,我是否应该将所有这些文件包含在我的可执行文件中?
\nc++ ×10
cmake ×8
pthreads ×6
boost ×3
makefile ×3
clion ×2
linux ×2
android ×1
android-ndk ×1
build ×1
c ×1
c++11 ×1
ubuntu-12.10 ×1
ubuntu-15.04 ×1
ubuntu-18.04 ×1