簡體   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