gpt4 book ai didi

c++ - CMake 包含错误(找不到文件, undefined reference )

转载 作者:行者123 更新时间:2023-11-28 04:57:15 25 4
gpt4 key购买 nike

<分区>

我正在创建一个使用 CMake 的项目。

我有一个用于标题的 include 文件夹和一个用于源代码的 src 文件夹。尝试使用 cmake --build . 构建项目时,出现了 "undefined reference" 错误:

/sdcard/dev/cpp/Bedrock++/src/main.cpp:(.text+0x70): undefined 
reference to `ChatFormat::Blue'

下面的附加细节:

$ cmake --build .
-- Configuring done
-- Generating done
-- Build files have been written to: /sdcard/dev/cpp/Bedrock++
Scanning dependencies of target Bedrock++
[ 33%] Building CXX object src/CMakeFiles/Bedrock++.dir/Server.cpp.o
[ 66%] Building CXX object src/CMakeFiles/Bedrock++.dir/main.cpp.o
[100%] Linking CXX executable Bedrock++
CMakeFiles/Bedrock++.dir/main.cpp.o: In function `main':
/sdcard/dev/cpp/Bedrock++/src/main.cpp:(.text+0x70): undefined
reference to `ChatFormat::Blue'
clang-5.0: error: linker command failed with exit code 1 (use -v to see
invocation)
make[2]: *** [src/CMakeFiles/Bedrock++.dir/build.make:121:
src/Bedrock++] Error 1
make[1]: *** [CMakeFiles/Makefile2:86:
src/CMakeFiles/Bedrock++.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

项目根CMakeLists.txt

cmake_minimum_required(VERSION 2.8.7)

# Build all dependent libraries as static

project(Bedrock++)

#add_subdirectory(lib/json)

add_subdirectory(src)
add_subdirectory(include)

src/CMakeLists.txt

project(bedrock++)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

include_directories("${PROJECT_SOURCE_DIR}/include")

file(GLOB SOURCES "*.cpp")

add_executable(${PROJECT_NAME} ${SOURCES})

include/CMakeLists.txt

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" FILES_MATCHING PATTERN "*.h")

main.cpp

#include <iostream>
#include <string>
#include "util/ChatFormat.h"

int main()
{
std::cout << "Hello World! " << ChatFormat::Blue << "hey" << std::endl;
}

ChatFormat.h header 位于 include/util/ChatFormat.h 中,并且针对每种不同的颜色包含以下内容:

static const std::string Black;

我曾尝试自己解决问题,但在 Internet 上进行了数小时的研究后被证明是一条死胡同。我希望我没有遗漏任何明显的东西。

如有任何帮助,我们将不胜感激。

25 4 0
文章推荐: html - 将图像放在 td 的中间
文章推荐: javascript - Pug - 从 JSON 对象检索 key
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com