简体   繁体   English

CMake未解决的外部

[英]CMake unresolved externals

I am trying to compile a very simple CMake project using Visual Studio 2013, however I am getting the following error upon trying to compile it: 我正在尝试使用Visual Studio 2013编译一个非常简单的CMake项目,但是在尝试对其进行编译时出现以下错误:

error LNK1120: 1 unresolved externals   cmake_issue\build\Debug\cmake_issue.exe 1   1   cmake_issue
error LNK2001: unresolved external symbol "public: static int Other::value" (?value@Other@@2HA) cmake_issue\build\test.obj  cmake_issue

I have a base directory with the following CMakeLists.txt in: 我有一个基本目录,其中包含以下CMakeLists.txt

project(cmake_issue)
add_subdirectory(other)
add_executable(cmake_issue src/test.cc)
target_link_libraries(cmake_issue other)

And the contents of src/test.cc : 以及src/test.cc的内容:

#include <cstdio>

#include "other/other.h"

int main(int argc, char *argv[]) {
    printf("value = %d\n", Other::value);

    return 0;
}

And a subdirectory called other with the following CMakeLists.txt in: 还有一个名为other的子目录,其中包含以下CMakeLists.txt

add_library(other SHARED src/other.cc)
target_include_directories(other PUBLIC include)
target_link_libraries(other)

And the contents of other/include/other/other.h : 以及other/include/other/other.h

#ifndef _OTHER_H_
#define _OTHER_H_

class __declspec(dllexport) Other {
public:
    static int value;
};

#endif

And the contents of other/src/other.cc : 以及other/src/other.cc的内容:

#include "other/other.h"

int Other::value = 30;

If I build the project with cmake and then open the generated sln in Visual Studio, both of the projects appear in the Solution Explorer. 如果我使用cmake构建项目,然后在Visual Studio中打开生成的sln,则这两个项目都将出现在解决方案资源管理器中。

If I right click and build other , it builds fine. 如果我右键单击并构建other ,则构建良好。 However if I try and build cmake_issue , I get the errors above. 但是,如果我尝试构建cmake_issuecmake_issue收到上述错误。 It looks like the cmake_issue solution is not using the other.dll (or other.lib ) files being generated when compiling the other solution. 看起来cmake_issue解决方案未在编译other解决方案时使用正在生成的other.dll (或other.lib )文件。

I can upload a zip of the source if it is needed. 如果需要,我可以上传源的zip。

Okay, the problem is not on CMake side, but with C++. 好的,问题不在CMake方面,而是C ++。 When you used dllexport'ed class in your executable, its definintion should read class __declspec(dllimport) Other . 在可执行文件中使用dllexport'ed类时,其定义应读为class __declspec(dllimport) Other This code works fine, for example: 该代码可以正常工作,例如:

#include <cstdio>

class __declspec(dllimport) Other {
public:
    static int value;
    int a();
};

int main(int argc, char *argv[]) {
    printf("value = %d\n", Other::value);

    return 0;
}

Here's a link with complete solution: https://social.msdn.microsoft.com/Forums/en-US/6c43599d-6d9d-4709-abf5-4d1e3f5e4fc9/exporting-static-class-members 这是完整解决方案的链接: https : //social.msdn.microsoft.com/Forums/en-US/6c43599d-6d9d-4709-abf5-4d1e3f5e4fc9/exporting-static-class-members

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

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