[英]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_issue
, cmake_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.