将 cmake 与共享(动态)库一起使用
Using cmake with a shared (dynamic) library
我正在尝试使用一个简单的共享库,它是我用一个只包含一个主要方法的文件制作的。
我首先 运行 cmake .
工作正常并且没有 return 任何错误。
然后我 运行 make
但是得到这个错误:
$ make
Scanning dependencies of target myprog
[ 50%] Building C object CMakeFiles/myprog.dir/main.c.o
[100%] Linking C executable myprog.exe
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lhello-user
collect2: error: ld returned 1 exit status
clang-3.8: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/myprog.dir/build.make:95: myprog.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myprog.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
CMakeLists.txt
文件
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
set(CMAKE_C_COMPILER clang)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
库存在于 /usr/local/lib/
中,作为 libhello-user.dll.a
注意:我将 Cygwin
用于 cmake
和 make
将我的评论变成答案
参见CMake/Tutorials/Exporting and Importing Targets。
您要么:
- 为库命名一个完整路径
- CMake 没有自动搜索它
- 您必须添加类似
find_library(_lib_path NAMES hello-user)
的内容
或者 - 更好 - 将它们放入 IMPORTED 目标中
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
add_library(hello-user SHARED IMPORTED GLOBAL)
set_target_properties(
hello-user
PROPERTIES
IMPORTED_LOCATION /usr/local/lib/libhello-user.dll
IMPORTED_IMPLIB /usr/local/lib/libhello-user.dll.a
)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
我正在尝试使用一个简单的共享库,它是我用一个只包含一个主要方法的文件制作的。
我首先 运行 cmake .
工作正常并且没有 return 任何错误。
然后我 运行 make
但是得到这个错误:
$ make
Scanning dependencies of target myprog
[ 50%] Building C object CMakeFiles/myprog.dir/main.c.o
[100%] Linking C executable myprog.exe
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lhello-user
collect2: error: ld returned 1 exit status
clang-3.8: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/myprog.dir/build.make:95: myprog.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myprog.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
CMakeLists.txt
文件
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
set(CMAKE_C_COMPILER clang)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
库存在于 /usr/local/lib/
中,作为 libhello-user.dll.a
注意:我将 Cygwin
用于 cmake
和 make
将我的评论变成答案
参见CMake/Tutorials/Exporting and Importing Targets。
您要么:
- 为库命名一个完整路径
- CMake 没有自动搜索它
- 您必须添加类似
find_library(_lib_path NAMES hello-user)
的内容
或者 - 更好 - 将它们放入 IMPORTED 目标中
cmake_minimum_required(VERSION 2.8.8) project(LIB_EXAMPLE) add_library(hello-user SHARED IMPORTED GLOBAL) set_target_properties( hello-user PROPERTIES IMPORTED_LOCATION /usr/local/lib/libhello-user.dll IMPORTED_IMPLIB /usr/local/lib/libhello-user.dll.a ) add_executable(myprog main.c) target_link_libraries(myprog hello-user)