2017-02-20 450 views
0

嗨即時嘗試使用一個簡單的共享庫,我用一個文件,只包含一個主。 (我第一次運行cmake .它工作得很好,並沒有返回任何錯誤)使用共享(動態)庫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) 

圖書館的/usr/local/lib/內部存在爲libhello-user.dll.a

注:我用cmake和make使用cygwin。

+1

的可能的複製[如何正確地創建一個模塊化項目的CMake文件(http://stackoverflow.com/questions/41519666/how-to-correctly-create-a-cmake-file - 對於-A模塊化項目)。參見[CMake /教程/導出和導入目標](https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets)。我可能會添加一些以'add_library(hello-user SHARED IMPORTED GLOBAL)'開頭的東西。 – Florian

+0

但我沒有創建一個新的庫?即時嘗試使用當前的一個。 ? – Mark

+0

是的,但解決方案是一樣的。請參閱下面的答案。 – Florian

回答

1

談到我的意見爲答案

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) 
    
+0

謝謝你的作品。你能解釋爲什麼我的方式不起作用嗎? – Mark