使用Cmake

时间:2018-11-11 10:00:37

标签: cmake compilation gsl pybind11

我正在将GNU GSL与pybind11模块链接。如果将GSL链接为共享库,则可以使用:

cmake_minimum_required(VERSION 2.8.12)
project(st)

# Paths
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

# Packages
add_subdirectory(pybind11)
find_package(GSL REQUIRED)

# Includes
set(DIRS ${GSL_INCLUDE_DIRS} ${GSLCBLAS_INCLUDE_DIRS})
include_directories(${DIRS})

# Python module
pybind11_add_module(st src/st.cpp)

# Libraries
set(LIBS ${LIBS} ${GSL_LIBRARIES} ${GSLCBLAS_LIBRARIES})
target_link_libraries(st PRIVATE ${LIBS})

如何静态链接?我尝试了很多不同的选择,但是没有用。

1 个答案:

答案 0 :(得分:1)

问题在于CMake在CMAKE_FIND_LIBRARY_SUFFIXES语句期间重置了PROJECT的内容,请参阅CMake安装中的文件 Modules / CMakeGenericSystem.cmake 。例如,此 CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

message("CMAKE_FIND_LIBRARY_SUFFIXES = ${CMAKE_FIND_LIBRARY_SUFFIXES}")
project(st)
message("CMAKE_FIND_LIBRARY_SUFFIXES = ${CMAKE_FIND_LIBRARY_SUFFIXES}")

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")  # <--- this is needed to replace the default

# Packages
find_package(GSL REQUIRED)

# Libraries
message("Libraries: ${GSL_LIBRARIES} ${GSLCBLAS_LIBRARIES}")

执行为

cmake -D CMAKE_FIND_LIBRARY_SUFFIXES=".a" .

产生输出

CMAKE_FIND_LIBRARY_SUFFIXES = .a
-- The C compiler identification is GNU 9.2.1
-- The CXX compiler identification is GNU 9.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMAKE_FIND_LIBRARY_SUFFIXES = .so;.a
-- Found PkgConfig: /usr/bin/pkg-config (found version "1.6.3") 
-- Found GSL: /opt/gsl-2.6/include (found version "2.6") 
Libraries: /opt/gsl-2.6/lib64/libgsl.a;/opt/gsl-2.6/lib64/libgslcblas.a 
-- Configuring done
-- Generating done

很显然,一种可能的解决方案似乎是在PROJECT语句之后重新定义变量(通过手动修改脚本),如上面的示例代码中所做的那样。