使CMake为MSVC构建共享库 [英] Getting CMake to build shared library for MSVC

查看:112
本文介绍了使CMake为MSVC构建共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用CMake在Unix环境中构建C和C ++项目。但是,我也想开始在MSVC中进行编译,但我想尽一切办法来获取cmake文档,但是却陷入困境。
尝试构建时始终收到以下消息。

I've been building C and C++ projects in unix environments using CMake. However, I want to also start compiling in MSVC and I'm trying to get my head around the cmake documentation but I'm getting stuck. I keep getting the following message when I try to build.


LINK:致命错误LNK1104:无法打开文件 Debug \MyLibrary.lib'[C:\沙箱\项目\cpp\DummyChelloWorld\build\ma
inProgram.vcxproj]

LINK : fatal error LNK1104: cannot open file 'Debug\MyLibrary.lib' [C:\sandbox\projects\cpp\DummyChelloWorld\build\ma inProgram.vcxproj]

您能告诉我我在做什么吗?

Can you tell me what I'm doing wrong?

CMakeLists.txt

CMakeLists.txt

    cmake_minimum_required(VERSION 3.4)
    project(helloWorld)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    include(GenerateExportHeader)

    ADD_LIBRARY(MyLibrary SHARED myShared.cpp)

    set(SOURCE_FILES main.cpp)

    GENERATE_EXPORT_HEADER( MyLibrary
                 BASE_NAME MyLibrary
                 EXPORT_MACRO_NAME MyLibrary_EXPORT
                 EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/MyLibrary_Export.h
                 STATIC_DEFINE MyLibrary_BUILT_AS_STATIC
            )

    add_executable(mainProgram ${SOURCE_FILES})
    TARGET_LINK_LIBRARIES(mainProgram MyLibrary)
    TARGET_INCLUDE_DIRECTORIES(mainProgram PUBLIC exports)

main.cpp

#include "myShared.h"

int main() {
  sayHI();
  return 0;
}

myShared.cpp

myShared.cpp

#include <iostream>
#include "myShared.h"

using namespace std;

void sayHI() {
  cout << "Hello World lib" << endl;
}

myShared.h

myShared.h

    #ifndef HELLOWORLD_HELLO_H
    #define HELLOWORLD_HELLO_H

    void sayHI();

    #endif //HELLOWORLD_HELLO_H


推荐答案

将我的评论变成答案

对于CMake版本> = 3.4

只需使用 CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS

cmake_minimum_required(VERSION 3.4)
project(helloWorld)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS 1)

add_library(MyLibrary SHARED myShared.cpp)

set(SOURCE_FILES main.cpp)
add_executable(mainProgram ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(mainProgram MyLibrary)

对于CMake版本< 3.4

您需要将函数/符号声明为已导出。因此,在您的情况下,您必须修改以下文件:

You need to declare your functions/symbols as exported. So in your case, you have to modify the following files:

myShared.h

#ifndef HELLOWORLD_HELLO_H
#define HELLOWORLD_HELLO_H

#include "MyLibrary_Export.h"

void MyLibrary_EXPORT sayHI();

#endif //HELLOWORLD_HELLO_H

CMakeLists.txt

cmake_minimum_required(VERSION 3.4)
project(helloWorld)

set(CMAKE_CXX_STANDARD 11)
include(GenerateExportHeader)

add_library(
    MyLibrary 
    SHARED 
        myShared.cpp
        myShared.h
        MyLibrary_Export.h   
)
GENERATE_EXPORT_HEADER( 
    MyLibrary
    BASE_NAME MyLibrary
    EXPORT_MACRO_NAME MyLibrary_EXPORT
    EXPORT_FILE_NAME MyLibrary_Export.h
    STATIC_DEFINE MyLibrary_BUILT_AS_STATIC
)
target_include_directories(MyLibrary PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")

set(SOURCE_FILES main.cpp)
add_executable(mainProgram ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(mainProgram MyLibrary)

我已经


  1. 在您的函数声明中添加了 MyLibrary_EXPORT

  2. 添加了 CMAKE_CURRENT_BINARY_DIR 到共享库的路径(指向生成的导出定义标题)

  3. 使用了更多跨平台的 CMAKE_CXX_STANDARD 定义。

  1. added MyLibrary_EXPORT to your function declaration
  2. added the CMAKE_CURRENT_BINARY_DIR to the shared libraries include paths (to point to the generated export definitions header)
  3. made use of the more cross-platform CMAKE_CXX_STANDARD definition.

替代

参考

  • cmake link shared library on Windows
  • Export all symbols when creating a DLL

这篇关于使CMake为MSVC构建共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆