簡體   English   中英

CMake未解決的外部

[英]CMake unresolved externals

我正在嘗試使用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_issuecmake_issue收到上述錯誤。 看起來cmake_issue解決方案未在編譯other解決方案時使用正在生成的other.dll (或other.lib )文件。

如果需要,我可以上傳源的zip。

好的,問題不在CMake方面,而是C ++。 在可執行文件中使用dllexport'ed類時,其定義應讀為class __declspec(dllimport) Other 該代碼可以正常工作,例如:

#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;
}

這是完整解決方案的鏈接: 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