2017-02-20 93 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