CMake未解决的外部

时间:2015-08-20 01:19:01

标签: c++ visual-studio-2013 cmake linker-errors

我正在尝试使用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

我的基本目录中包含以下CMakeLists.txt

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

src/test.cc的内容:

#include <cstdio>

#include "other/other.h"

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

    return 0;
}

一个名为other的子目录,其中包含以下CMakeLists.txt

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

other/include/other/other.h的内容:

#ifndef _OTHER_H_
#define _OTHER_H_

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

#endif

other/src/other.cc的内容:

#include "other/other.h"

int Other::value = 30;

如果我使用cmake构建项目,然后在Visual Studio中打开生成的sln,则两个项目都会显示在解决方案资源管理器中。

如果我右键单击并构建other,则构建正常。但是,如果我尝试构建cmake_issue,我会得到上面的错误。看起来cmake_issue解决方案没有使用编译other.dll解决方案时生成的other.lib(或other)文件。

如果需要,我可以上传源代码的zip。

1 个答案:

答案 0 :(得分:2)

好的,问题不在于CMake,而在于C ++。在可执行文件中使用dllexport&#39; ed类时,其定义应为function show_dropdown(dropdown_id, dropdown_menu_id) { var dropdown = document.getElementById(dropdown_id); var menu = document.getElementById(dropdown_menu_id); var menu_entries = Array.prototype.slice.call(menu.childNodes); var dropdown_top = parseInt(window.getComputedStyle(dropdown).top); ... } 。此代码工作正常,例如:

class __declspec(dllimport) Other

这是一个完整解决方案的链接:https://social.msdn.microsoft.com/Forums/en-US/6c43599d-6d9d-4709-abf5-4d1e3f5e4fc9/exporting-static-class-members

相关问题