C++ CMake:将可执行文件链接到多个库(*.so)
我不熟悉CMake的建筑。我正在使用Ubuntu,我有一个.cpp文件(比如位于~/mydir某处的xyz.cpp),它链接到三个自定义共享库(libA.so、libB.so和libC.so库)。这三个*.so文件位于C++ CMake:将可执行文件链接到多个库(*.so),c++,cmake,C++,Cmake,我不熟悉CMake的建筑。我正在使用Ubuntu,我有一个.cpp文件(比如位于~/mydir某处的xyz.cpp),它链接到三个自定义共享库(libA.so、libB.so和libC.so库)。这三个*.so文件位于/usr/local/lib中 我想创建一个CMakeLists.txt来编译它。下面的脚本会引发如下错误: cmake_minimum_required(VERSION 3.11) project(xyz VERSION 1.0.0 LANGUAGES C CXX) # Set
/usr/local/lib
中
我想创建一个CMakeLists.txt来编译它。下面的脚本会引发如下错误:
cmake_minimum_required(VERSION 3.11)
project(xyz VERSION 1.0.0 LANGUAGES C CXX)
# Set C standard to C11
set(CMAKE_C_STANDARD 11)
set(SOURCES src/xyz.cpp)
include_directories(/usr/local/include)
#For the shared library:
target_link_libraries(libA -L/usr/local/lib/)
target_link_libraries(libB -L/usr/local/lib/)
target_link_libraries(libC -L/usr/local/lib/)
target_link_libraries(xyz libA libB libC )
add_executable(xyz src/xyz.cpp)
错误:
CMake Error at CMakeLists.txt: (target_link_libraries):
Cannot specify link libraries for target "libA" which is not built
by this project.
-- Configuring incomplete, errors occurred!
有几个重要的注意事项:
add\u library()
或add\u executable()
创建。因此,任何target\u link\u libraries()
目标链接库()
即可将所有*库链接到可执行文件。前三个调用是不必要的(并且无效,因为libA
、libB
和libC
都未定义)。您应该提供每个库的完整路径,以确保使用正确的库
target.*
调用中包括作用域参数是一种很好的做法,例如target\u link\u libraries()
。这指定库是链接到(PRIVATE
),还是添加到链接接口(interface
),还是两者都链接到(PUBLIC
)。随着您的CMake项目不断扩大,以包含更多的目标,这种区别变得更加重要cmake_minimum_required(VERSION 3.11)
# No need to specify C and CXX for LANGUAGES here, these are enabled by default!
project(xyz VERSION 1.0.0)
# Set C standard to C11
set(CMAKE_C_STANDARD 11)
set(SOURCES src/xyz.cpp)
include_directories(/usr/local/include)
# Define your executable target. You can pass in the SOURCES variable defined above.
add_executable(xyz ${SOURCES})
# Link the other libraries to your executable, using their full path.
# Note, the '-L' flag is not necessary.
target_link_libraries(xyz PRIVATE
/usr/local/lib/libA.so
/usr/local/lib/libB.so
/usr/local/lib/libC.so
)
有几个重要的注意事项:
add\u library()
或add\u executable()
创建。因此,任何target\u link\u libraries()
目标链接库()
即可将所有*库链接到可执行文件。前三个调用是不必要的(并且无效,因为libA
、libB
和libC
都未定义)。您应该提供每个库的完整路径,以确保使用正确的库
target.*
调用中包括作用域参数是一种很好的做法,例如target\u link\u libraries()
。这指定库是链接到(PRIVATE
),还是添加到链接接口(interface
),还是两者都链接到(PUBLIC
)。随着您的CMake项目不断扩大,以包含更多的目标,这种区别变得更加重要cmake_minimum_required(VERSION 3.11)
# No need to specify C and CXX for LANGUAGES here, these are enabled by default!
project(xyz VERSION 1.0.0)
# Set C standard to C11
set(CMAKE_C_STANDARD 11)
set(SOURCES src/xyz.cpp)
include_directories(/usr/local/include)
# Define your executable target. You can pass in the SOURCES variable defined above.
add_executable(xyz ${SOURCES})
# Link the other libraries to your executable, using their full path.
# Note, the '-L' flag is not necessary.
target_link_libraries(xyz PRIVATE
/usr/local/lib/libA.so
/usr/local/lib/libB.so
/usr/local/lib/libC.so
)
1) 目标链接库
应遵循添加可执行文件
;2) 您根本不需要目标链接库(libA…
;3) 要添加链接目录,请使用target\u link\u directories
.1)target\u link\u库
应遵循add\u executable
;2) 您根本不需要目标链接库(libA…
;3) 要添加链接目录,请使用目标链接目录
。