C ++ CMake构建错误“未定义的引用”

时间:2018-04-13 17:14:44

标签: c++ cmake

我是CMake的新手,我正在尝试使用VS2017和WSL(Linux子系统)构建一个跨平台的c ++项目。不幸的是,我仍然试图理解CMake如何处理依赖关系和链接,所以我有很多“未解析的外部符号”和“未定义的引用...”错误。

为了更容易理解我创建了下面的项目,我能够复制我遇到的那种错误。需要一些帮助才能更好地了解如何配置CMake。

注意:我刚刚在那里包含了opencv文件夹,以显示我是如何尝试使用CMake包含外部依赖项的,尽管我在这个示例中并没有真正使用任何opencv代码。

Project Folder Structure

/CMakeLists.txt
cmake_minimum_required(VERSION 3.9)

project(WslSample) set (CMAKE_CXX_STANDARD 11)

include_directories(
    ${PROJECT_BINARY_DIR}
    ${PROJECT_SOURCE_DIR}/ext/opencv/include )

add_subdirectory(src)
/src/CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project (SampleApp)
set (CMAKE_CXX_STANDARD 11)

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

add_executable(${PROJECT_NAME} main.cpp ${HEADERS} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC utils)
/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.

1 个答案:

答案 0 :(得分:0)

发现问题...我寻找文件的方式是错误的,我使用了错误的CMake变量。在这个具体案例的解决方案之下:

/src/CMakeLists.txt

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