[英]How do I link a library using CMake?
I'm trying to add a new library to a project built using CMake and am having trouble.我正在尝试将新库添加到使用 CMake 构建的项目中,但遇到了麻烦。 I'm trying to follow this .
我正在尝试遵循这一点。 I've made a test project that looks like this:
我做了一个如下所示的测试项目:
cmake_test/
test.cpp
CMakeLists.txt
liblsl/
include/
lsl_cpp.h
CMakeLists.txt
liblsl64.dll
liblsl64.so
build/
the CMakeLists.txt in cmake_test looks like this: cmake_test 中的 CMakeLists.txt 如下所示:
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(Tutorial test.cpp)
add_subdirectory(liblsl)
target_link_libraries(Tutorial PUBLIC ${LSL_LIBRARY})
and the CMakeLists.txt in liblsl looks like this: liblsl 中的 CMakeLists.txt 如下所示:
find_path(LSL_INCLUDE_DIR lsl_cpp.h)
find_library(LSL_LIBRARY liblsl64)
include_directories(${LSL_INCLUDE_DIR})
But I keep getting the error No rule to make target '.../liblsl64.lib', needed by 'Tutorial.exe'. Stop.
但是我不断收到错误
No rule to make target '.../liblsl64.lib', needed by 'Tutorial.exe'. Stop.
No rule to make target '.../liblsl64.lib', needed by 'Tutorial.exe'. Stop.
Any idea what I'm doing wrong?知道我做错了什么吗? I'm on Windows 10 using mingw-w64 v5.4.0 if that makes any difference.
如果这有什么不同的话,我在 Windows 10 上使用 mingw-w64 v5.4.0。
CMakeLists.txt
in cmake_test
: cmake_test
中的CMakeLists.txt
:
cmake_minimum_required(VERSION 3.10)
project(Tutorial VERSION 1.0)
add_subdirectory(liblsl)
add_executable(Tutorial test.cpp)
target_compile_features(Tutorial PUBLIC cxx_std_11)
target_link_libraries(Tutorial PUBLIC liblsl)
CMakeLists.txt
in liblsl
: liblsl
中的CMakeLists.txt
:
add_library(liblsl SHARED IMPORTED GLOBAL)
set_target_properties(liblsl PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
set_target_properties(liblsl PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/liblsl64.so")
For Windows use:对于 Windows 使用:
set_target_properties(liblsl PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/liblsl64.dll")
set_target_properties(liblsl PROPERTIES IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/liblsl64.lib")
In add_library
, you say SHARED
because your library is a shared one ( so
/ dll
), you say IMPORTED
because you don't want to build the library, and you say GLOBAL
because you want it to be visible outside liblsl
.在
add_library
中,您说SHARED
因为您的库是共享库( so
/ dll
),您说IMPORTED
是因为您不想构建库,而您说GLOBAL
是因为您希望它在liblsl
之外可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.