【问题标题】:CMake: satisfying dependencies when building shared object file (.so) from object file (.o)CMake:从对象文件(.o)构建共享对象文件(.so)时满足依赖关系
【发布时间】:2014-03-03 03:24:36
【问题描述】:

我可以在没有 CMake 的情况下使用手写的 Makefile 来做到这一点,如下所示:

g++  $(CXSCINC) -c -fPIC cellComplex_extern.cpp -o cellComplex_extern.o  
g++  $(CXSCINC) -shared -Wl -o cellComplex_lib.so cellComplex_extern.o $(CXSCLIB) -lcxsc

这让我共享库cellComplex_lib.so,然后被ctypes 拾取为动态链接库(lib = ctypes.cdll.LoadLibrary('./cellComplex_lib.so')供以后使用。

我的项目已作为构建系统迁移到 CMake,我希望模拟上面的 Makefile 的功能。

到目前为止,我已经发现了用于 CMakeLists.txt 的 add_library() 命令,但从未建立到 CXSC 库的链接(正如我在 cmake 之后运行 make 时出现的可怕抱怨所证明的那样。

如何告诉 CMake 使用第三方库 CXSC 构建 cellComplex_lib

-- 不工作CMakeLists.txt--

add_library(include/python/cellComplex_extern OBJECT
            include/python/cellComplex_extern.cpp ${all_headers})

add_library(include/python/cellComplex_lib SHARED
            include/python/cellComplex_extern)
target_link_libraries(include/python/cellComplex_lib ${CXSC_LIB_DIR}/libcxsc.a)

运行cmake后make的结果:

.
.
.
[ 75%] Built target include/python/cellComplex_extern
Linking CXX shared library libinclude/python/cellComplex_lib.dylib
ld: can't open output file for writing: libinclude/python/cellComplex_lib.dylib, errno=2 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libinclude/python/cellComplex_lib.dylib] Error 1
make[1]: *** [CMakeFiles/include/python/cellComplex_lib.dir/all] Error 2
make: *** [all] Error 2

【问题讨论】:

  • 您现在好像遇到了路径问题。还是正在使用该库?

标签: cmake


【解决方案1】:

我认为你需要使用target_link_libraries

target_link_libraries(include/python/cellComplex_lib ${CXSLIB})

这是我在 Win32 开发过程中使用的:

link_directories(${LIB_ROOT_DIR}/lib ${LIB_ROOT_DIR2}/lib/morelibs)
add_library(MyDll1 SHARED File1.cpp File2.cpp)
add_library(MyDll2 SHARED File3.cpp File4.cpp)
add_dependencies(MyDll2 Dll1)
target_link_libraries(MyDll2 Dll1 some.lib another.lib)

在这里您指定 Dll2 需要 Dll1 和其他两个外部库。

【讨论】:

  • 当目标是我刚刚使用add_library 添加的库时,我似乎无法使用target_link_libraries()。错误是ld: can't open output file for writing: <target_lib>。我尝试了更多的东西并编辑了我的问题。不过谢谢,我认为这与我正在寻找的非常接近。
  • 这种情况你需要添加add_dependency,我会添加到答案中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 2011-09-08
  • 1970-01-01
相关资源
最近更新 更多