我可以使用手写的Makefile在没有CMake的情况下执行此操作,如下所示:
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库的链接(在make
之后运行cmake
时出现了可怕的抱怨。 。
如何告诉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
答案 0 :(得分: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和另外两个外部lib。