【发布时间】:2014-12-17 04:49:44
【问题描述】:
这是一个非常微不足道的问题,可能是由于我对 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 将库引用设置为共享?
谢谢,
【问题讨论】:
-
在这一行:
include_directories("${PROJECT_SOURCE_DIR}/MathFuncs}")是不是多了一个}? -
您是否验证过 MathFuncs 构建? (构建失败不会使教程停止尝试构建,但链接文件将不存在)。
-
MathFuncs 仅在 dll 中构建没有问题。我现在按照 Frasers 的回答理解,第一步需要导出定义,但是我仍然在生成 .lib 时遇到问题。谢谢