使用CMake链接到Windows上的静态库的麻烦

时间:2016-02-19 08:36:10

标签: c++ c cmake static-libraries libraries

LS,

我正在尝试构建一个静态和动态库,可用于动态和静态链接。无论使用何种编译器,我都希望该库能够在尽可能多的平台上运行。为了构建库和一些测试程序,我正在使用CMake在Linux和Windows上分别使用g ++和MSVC ++构建库eeg。

在Linux上,动态和静态库似乎都像我怀疑的那样工作,在Windows上.dll似乎完美链接并且我的测试程序运行。但是,使用静态库的程序会抱怨链接错误。我真的很想念我做错了什么,它可能是我的CMakeLists.txt中的内容,也可能是我的库的安装程序。 下面我做了一个使用我的库的最小程序,它演示了我遇到的问题。该库包含两个C ++文件,一个用于导出C ++文件中的代码的C API,一个使用C API的C ++程序,以及一个可以构建所有程序但使用静态库的程序的CMakeList.txt。这一切都产生了一个美妙的“你好,世界!”。

我知道我展示了很多代码,但至少它是一个最小的项目,它演示了我链接到静态库的问题。我希望有人能够好好看看这个项目并向我解释我做错了什么。

亲切的问候,

hetepeperfan

C ++文件PriCpp.cpp

#include "PriCpp.h"

using namespace std;
string PriMessageBuilder::message() const {
    return "Hello, World!";
}

标题PriCpp.h

#ifndef PRICPP_H
#define PRICPP_H

#include <string>

class PriMessageBuilder{
public:
    std::string message() const;
};

#endif

C API是: mycapi.h

#ifndef MYCAPI_H
#define MYCAPI_H
#include "builder_export.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {} message_builder;

BUILDER_EXPORT message_builder* message_builder_new();
BUILDER_EXPORT void             message_builder_destroy(
                                        message_builder* builder
                                        );
BUILDER_EXPORT char*            message_builder_message(
                                        message_builder* builder
                                        );
#ifdef __cplusplus
}
#endif

#endif

mycapi.cpp是:

#include "mycapi.h"
#include "PriCpp.h"
#include <cstring>
#include <cstdlib>

message_builder* message_builder_new() {
    PriMessageBuilder* ret = NULL;
    try {
        ret = new PriMessageBuilder();
    } catch (...) {
    }
    return reinterpret_cast<message_builder*>(ret);
}

void message_builder_destroy(message_builder* builder)
{
    PriMessageBuilder* b = reinterpret_cast<PriMessageBuilder*>(builder);
    delete b;
}

char* message_builder_message(message_builder* builder)
{
    PriMessageBuilder* b = reinterpret_cast<PriMessageBuilder*>(builder);
    return strdup(b->message().c_str());
}

我与上面的库结合使用的程序。 program.cpp

#include <iostream>
#include <cstdlib>
#include <string>
#include "mycapi.h"

using namespace std;

class MessageBuilder {
public:
    MessageBuilder() : m_builder(message_builder_new()) {
    }
    ~MessageBuilder() {
        message_builder_destroy(m_builder);
    }
    string MessageBuilder::message() {
        char* msg = message_builder_message(m_builder);
        string m(msg);
        free (msg);
        return m;
    }
private:
    message_builder* m_builder;
};

int main(int, char**) {
    MessageBuilder m;
    cout << m.message() << endl;
    return 0;
}

最后用CMakeLists.txt构建makefile或visual studio解决方案

#setting up project
cmake_minimum_required(VERSION 2.6)
project(libbuilder CXX) 
include (GenerateExportHeader)

include_directories("${PROJECT_BINARY_DIR}")

#creating library
add_library(builder SHARED PriCpp.cpp PriCpp.h mycapi.cpp mycapi.h)
add_library(builder-static STATIC PriCpp.cpp PriCpp.h mycapi.cpp mycapi.h)

generate_export_header(builder)

set_target_properties(builder PROPERTIES COMPILE_FLAGS -DBUILD_BUILDER_SHARED)
set_target_properties(builder-static PROPERTIES COMPILE_FLAGS -DBUILDER_STATIC_DEFINE)

#creating programs
add_executable(program program.cpp)
add_executable(program-static program.cpp)

target_link_libraries(program builder)
target_link_libraries(program-static builder-static)

如果我构建解决方案,我会收到这些链接器错误:

1>program.obj : error LNK2019: unresolved external symbol __imp__message_builder_new referenced in function "public: __thiscall MessageBuilder::MessageBuilder(void)" (??0MessageBuilder@@QAE@XZ)
1>program.obj : error LNK2019: unresolved external symbol __imp__message_builder_destroy referenced in function "public: __thiscall MessageBuilder::~MessageBuilder(void)" (??1MessageBuilder@@QAE@XZ)
1>program.obj : error LNK2019: unresolved external symbol __imp__message_builder_message referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall MessageBuilder::message(void)" (?message@MessageBuilder@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>K:\duijn119\programming\cmake\staticsharedlib\build\Debug\program-static.exe : fatal error LNK1120: 3 unresolved externals

CMake生成的包含标题是:

#ifndef BUILDER_EXPORT_H
#define BUILDER_EXPORT_H

#ifdef BUILDER_STATIC_DEFINE
#  define BUILDER_EXPORT
#  define BUILDER_NO_EXPORT
#else
#  ifndef BUILDER_EXPORT
#    ifdef builder_EXPORTS
        /* We are building this library */
#      define BUILDER_EXPORT __declspec(dllexport)
#    else
        /* We are using this library */
#      define BUILDER_EXPORT __declspec(dllimport)
#    endif
#  endif

#  ifndef BUILDER_NO_EXPORT
#    define BUILDER_NO_EXPORT 
#  endif
#endif

#ifndef BUILDER_DEPRECATED
#  define BUILDER_DEPRECATED __declspec(deprecated)
#endif

#ifndef BUILDER_DEPRECATED_EXPORT
#  define BUILDER_DEPRECATED_EXPORT BUILDER_EXPORT BUILDER_DEPRECATED
#endif

#ifndef BUILDER_DEPRECATED_NO_EXPORT
#  define BUILDER_DEPRECATED_NO_EXPORT BUILDER_NO_EXPORT BUILDER_DEPRECATED
#endif

#define DEFINE_NO_DEPRECATED 0
#if DEFINE_NO_DEPRECATED
# define BUILDER_NO_DEPRECATED
#endif

#endif

1 个答案:

答案 0 :(得分:4)

您需要在set_target_properties(program-static PROPERTIES COMPILE_FLAGS -DBUILDER_STATIC_DEFINE)之后添加add_executable(program-static program.cpp)。目前你的静态库正确构建,但应用程序获取的库标题错误declspec(好吧,根本不应该有declspec而你有一个)

此外,使用以下概念似乎更合适:

set_target_properties(program-static PROPERTIES COMPILE_DEFINITIONS BUILDER_STATIC_DEFINE)

COMPILE_DEFINITIONS代替COMPILE_FLAGS