簡體   English   中英

C ++ CMake構建錯誤“未定義參考”

[英]C++ CMake Build Errors “Undefined Reference”

我對CMake還是很陌生,我正在嘗試使用VS2017和WSL(Linux子系統)構建跨平台的c ++項目。 不幸的是,我仍然試圖了解CMake如何處理依賴關系和鏈接,因此我遇到了很多“未解決的外部符號”和“對...的未定義引用”錯誤。

為了更容易理解,我創建了以下項目,在該項目中我可以復制遇到的錯誤。 需要一些幫助以更好地了解如何配置CMake。

注意:盡管我在本示例中並未真正使用任何opencv代碼,但我只是在其中包括opencv文件夾,以顯示我如何嘗試使用CMake包括外部依賴項。

項目文件夾結構

/CMakeLists.txt
#include <iostream>
#include <string>
#include <log.hpp>

int main()
{
    log(LEVEL_INFO, "Sample App running...");
    return 0;
}
/src/CMakeLists.txt
18:06:55: Copying files to remote machine...
18:06:55: Finished copying files (elapsed time 00h:00m:00s:009ms).
cd '/var/tmp/build/3df28527-45dd-1335-b3fd-743d2a9ed7dd/build/Linux-Debug';/usr/local/bin/cmake --build "/var/tmp/build/3df28527-45dd-1335-b3fd-743d2a9ed7dd/build/Linux-Debug" --target SampleApp  ;

[ 50%] Linking CXX executable SampleApp
CMakeFiles/SampleApp.dir/main.cpp.o: In function `main':
/var/tmp/src/3df28527-45dd-1335-b3fd-743d2a9ed7dd/Linux-Debug/src/main.cpp:7: undefined reference to `log(LogLevel const&, char const*)'
collect2: error: ld returned 1 exit status
src/CMakeFiles/SampleApp.dir/build.make:94: recipe for target 'src/SampleApp' failed
make[3]: *** [src/SampleApp] Error 1
CMakeFiles/Makefile2:85: recipe for target 'src/CMakeFiles/SampleApp.dir/all' failed
make[2]: *** [src/CMakeFiles/SampleApp.dir/all] Error 2
CMakeFiles/Makefile2:97: recipe for target 'src/CMakeFiles/SampleApp.dir/rule' failed
make[1]: *** [src/CMakeFiles/SampleApp.dir/rule] Error 2
Makefile:118: recipe for target 'SampleApp' failed
make: *** [SampleApp] Error 2

Build failed.
/src/main.cpp
 #include <iostream> #include <string> #include <log.hpp> int main() { log(LEVEL_INFO, "Sample App running..."); return 0; } 
/utils/log.hpp
 #pragma once typedef enum { LEVEL_DEBUG = 0, LEVEL_INFO = 1, LEVEL_WARNING = 2, LEVEL_ERROR = 3, } LogLevel; void log(const LogLevel& level, const char* str); 
/utils/log.cpp
 #include <iostream> #include <log.hpp> std::string getLogStr(const LogLevel& level) { switch (level) { case LEVEL_INFO: return "INFO"; case LEVEL_DEBUG: return "DEBUG"; case LEVEL_ERROR: return "ERROR"; case LEVEL_WARNING: return "WARNING"; default: return "n/a"; } } void log(const LogLevel& level, const char* str) { std::cout << getLogStr(level) << " " << str << std::endl; } 
建立輸出日志
 18:06:55: Copying files to remote machine... 18:06:55: Finished copying files (elapsed time 00h:00m:00s:009ms). cd '/var/tmp/build/3df28527-45dd-1335-b3fd-743d2a9ed7dd/build/Linux-Debug';/usr/local/bin/cmake --build "/var/tmp/build/3df28527-45dd-1335-b3fd-743d2a9ed7dd/build/Linux-Debug" --target SampleApp ; [ 50%] Linking CXX executable SampleApp CMakeFiles/SampleApp.dir/main.cpp.o: In function `main': /var/tmp/src/3df28527-45dd-1335-b3fd-743d2a9ed7dd/Linux-Debug/src/main.cpp:7: undefined reference to `log(LogLevel const&, char const*)' collect2: error: ld returned 1 exit status src/CMakeFiles/SampleApp.dir/build.make:94: recipe for target 'src/SampleApp' failed make[3]: *** [src/SampleApp] Error 1 CMakeFiles/Makefile2:85: recipe for target 'src/CMakeFiles/SampleApp.dir/all' failed make[2]: *** [src/CMakeFiles/SampleApp.dir/all] Error 2 CMakeFiles/Makefile2:97: recipe for target 'src/CMakeFiles/SampleApp.dir/rule' failed make[1]: *** [src/CMakeFiles/SampleApp.dir/rule] Error 2 Makefile:118: recipe for target 'SampleApp' failed make: *** [SampleApp] Error 2 Build failed. 

發現了問題...我查找文件的方式錯誤,並且使用了錯誤的CMake變量。 在此特定情況下的解決方案下方:

/src/CMakeLists.txt

file(GLOB_RECURSE SOURCES RELATIVE ${PROJECT_SOURCE_DIR} "*.cpp")
file(GLOB_RECURSE HEADERS RELATIVE ${PROJECT_SOURCE_DIR} "*.hpp")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM