[英]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.