繁体   English   中英

将cmake与共享(动态)库一起使用

[英]Using cmake with a shared (dynamic) library

嗨,我尝试使用一个仅包含主文件的简单共享库。 (我第一次运行cmake .它工作正常,没有返回任何错误)

错误

$ 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)

该库作为libhello-user.dll.a存在于/usr/local/lib/ libhello-user.dll.a

注意:我将cygwin与cmake和make一起使用。

把我的评论变成答案

请参阅CMake /教程/导出和导入目标

您要么有:

  • 为库命名完整路径
    • CMake不会自动搜索
    • 您将必须添加诸如find_library(_lib_path NAMES hello-user)
  • 或者-更好地-将其放入“导入的目标”中

     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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM