CMake 在链接之前指定源
CMake specify sources before linking
我有以下CMakeLists:
cmake_minimum_required(VERSION 3.3)
project(untitled)
set(SOURCE_FILES main.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/kernael/.openmpi/include -pthread -Wl,-rpath -Wl,/home/kernael/.openmpi/lib -Wl,--enable-new-dtags -L/home/kernael/.openmpi/lib -lmpi_cxx -lmpi")
add_executable(untitled ${SOURCE_FILES})
但是构建似乎失败了,因为CMake在"-l"选项之后自动指定源(main.cpp),这似乎是问题所在,因为使用命令行时,以下命令有效:
g++ -I/home/kernael/.openmpi/include -pthread -L/home/kernael/.openmpi/lib main.cpp -lmpi_cxx -lmpi
但是这个没有,并且会产生与CMake构建相同的错误:
g++ -I/home/kernael/.openmpi/include -pthread -L/home/kernael/.openmpi/lib -lmpi_cxx -lmpi main.cpp
如何告诉 CMake 在链接发生之前指定源文件?
您需要调查以下 CMake 命令:
https://cmake.org/cmake/help/v3.3/command/target_include_directories.htmlhttps://cmake.org/cmake/help/v3.3/command/target_link_libraries.html
像这样的事情应该可以完成工作:
cmake_minimum_required(VERSION 3.3)
add_executable(untitled main.cxx)
target_include_directories(untitled PUBLIC /home/kernael/.openmpi/include)
target_link_libraries(untitled -pthread -L/home/kernael/.openmpi/lib -lmpi_cxx -lmpi)
不能将CMAKE_CXX_FLAGS
用于 CMake 中的包含,它仅用于编译器选项。
你必须找到 MPI 与 find_package
.然后,CMake 查找包含路径和库。
find_package(MPI)
if (MPI_C_FOUND)
include_directories(${MPI_INCLUDE_PATH})
add_executable(untitled ${SOURCE_FILES})
target_link_libraries(untitled ${MPI_LIBRARIES})
set_target_properties(untitled PROPERTIES
COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
else()
# MPI not found ...
endif()
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 无法链接 CMake 中的本地库
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseE
- 在 CMake 中为每个目标设置编译器/链接器标志
- 链接 cmake 时出现未定义的引用错误
- 如何将GTest与CMake一起使用?遵循谷歌指南时的链接问题
- 将 SDL2 与 CMake 链接起来
- CMake 库链接使用两个不同的编译器版本
- CMake 无法链接外部库
- CMake - 更改共享库链接选项
- CMake 3.5 中的链接器标志位置
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 如何避免在 cmake 中自动链接 Qt5 库?
- 将源中的 GLEW 与 cmake 静态链接
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- 找不到 CMake 链接的库
- Opencv 链接 CMAKE 不起作用 Ubuntu 16.04 Docker
- 如何在没有外部脚本的情况下链接CMake和SQLite
- 链接CMake的Openscenegraph库