繁体   English   中英

为什么我的共享库在使用CMake时无法正确链接?

[英]Why is my shared library not linking correctly when using CMake?

这是一个非常微不足道的问题,可能是由于我对CMake缺乏经验。 我已经按照http://fabzter.com/blog/cmake-tutorial-1上的教程进行操作,并且遇到链接问题。

基本上我有一个MathFuncs库和一个使用MathFuncs的可执行文件。 MathFuncs的CMakeLists是:

cmake_minimum_required(VERSION 2.8)
project(MathFuncs)

include_directories(${PROJECT_SOURCE_DIR})

SET (HEADER_FILES mysqrt.hpp)
add_library(MathFuncs SHARED mysqrt.cpp ${HEADER_FILES})

可执行的CMakeLists是:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)

set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
set (Tutorial_VERSION_BUGFIX 0)

#configure header file to pass some of the CMake settings 
#to the source code
configure_file(
    "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
    "${PROJECT_BINARY_DIR}/TutorialConfig.h"
) 

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")

# add the include directories necessary to build library
include_directories("${PROJECT_SOURCE_DIR}/MathFuncs}")
add_subdirectory(MathFuncs)

SET (MATHFUNCTIONS_DIR ${PROJECT_SOURCE_DIR}/MathFuncs)
add_executable(tutorial tutorial.cpp ${MATHFUNCTIONS_DIR}/mysqrt.hpp)

target_link_libraries(tutorial MathFuncs)

CMake运行良好。 但是,当我尝试使用Visual Studio进行编译时,我收到一个链接器错误,指出它无法打开MathFuncs.lib。 当我从MathFuncs CMakeList中删除'SHARED'选项时,它正在运行,因为它正在构建一个静态库,但是对于我的应用程序,我想要一个共享库DLL。

如何使CMake将库引用设置为共享?

谢谢,

看起来您没有正确地从DLL导出类/函数。 有关详细说明,请参阅此文章

这并不完全是直截了当的,但幸运的是CMake可以在这里提供一些帮助。 您可以使用GenerateExportHeader模块自动生成包含正确导出库的公共函数所需的所有预处理器定义的标头。

这是我如何更改文件的示例。 希望每个人都有足够的评论让你了解正在发生的事情。 如果没有,请添加评论,我将展开。

顶级CMakeLists.txt:

cmake_minimum_required (VERSION 3.0)
project(Tutorial)

add_subdirectory(MathFuncs)

set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
set(Tutorial_VERSION_BUGFIX 0)

# Configure header file to pass some of the CMake settings to the source code.
set(TutorialConfigHeader "${PROJECT_BINARY_DIR}/TutorialConfig.h")
configure_file(TutorialConfig.h.in "${TutorialConfigHeader}")

# Include the output file of 'configure_file' to ensure it gets configured.
add_executable(tutorial tutorial.cpp "${TutorialConfigHeader}")

# Add TutorialConfig.h's path to include dirs for 'tutorial'.
target_include_directories(tutorial PRIVATE "${PROJECT_BINARY_DIR}")

target_link_libraries(tutorial MathFuncs)

# If 'MathFuncs' is a shared lib, copy it to 'tutorial's bin dir so
# it can be found at runtime.
get_target_property(MathFuncsType MathFuncs TYPE)
if(MathFuncsType STREQUAL "SHARED_LIBRARY")
  add_custom_command(TARGET tutorial POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                         $<TARGET_FILE:MathFuncs> $<TARGET_FILE_DIR:tutorial>
                     COMMENT "Copying MathFuncs shared lib alongside tutorial."
                     VERBATIM)
endif()


MathFuncs /的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(MathFuncs)

add_library(MathFuncs SHARED
            mysqrt.cpp
            mysqrt.hpp
            "${PROJECT_BINARY_DIR}/mathfuncs_export.h")

# Write appropriate export PP definitions in a file called
# 'mathfuncs_export.h' in the current binary dir
include(GenerateExportHeader)
generate_export_header(MathFuncs)

# Add the current source dir as a PUBLIC include dir
# (to allow mysqrt.hpp to be found by dependent targets)
# Add the current binary dir as a PUBLIC include dir
# (to allow mathfuncs_export.h to be found by dependent targets)
target_include_directories(MathFuncs PUBLIC
                           ${PROJECT_SOURCE_DIR} ${PROJECT_BINARY_DIR})


然后要使用生成的导出头,你只需要让mysqrt.hpp包含如下内容:

#ifndef MYSQRT_HPP_
#define MYSQRT_HPP_

#include "mathfuncs_export.h"

double MATHFUNCS_EXPORT MySqrt(double input);

#endif  // MYSQRT_HPP_


这应该导致VS创建一个导出库${CMAKE_BINARY_DIR}/MathFuncs/Debug/MathFuncs.lib (修复链接器问题) 实际的DLL ${CMAKE_BINARY_DIR}/MathFuncs/Debug/MathFuncs.dll

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM