【发布时间】:2021-10-03 13:22:41
【问题描述】:
您好,我正在尝试使用 CMake 构建我的 SFML 项目,但是当我尝试使用多个源文件时,CMake 不会链接目标文件
这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
set(CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project(PolygonPoints)
find_package(SFML 2.5.1 COMPONENTS graphics window system audio REQUIRED)
file(
GLOB
SOURCES
"src/*.hpp"
"src/*.cpp"
)
add_executable(PolygonPoints ${SOURCES})
target_link_libraries(PolygonPoints sfml-graphics sfml-window sfml-system sfml-audio)
target_include_directories(PolygonPoints PRIVATE include)
这是我的项目结构:
PolygonPoints/
src/
main.cpp
point.cpp
include/
point.hpp
build/
(...)
CMakeLists.txt
(...)
这是我得到的错误:
bayram@milkyway ~/Projects/PolygonPoints/build (main)> cmake .. && make && ./PolygonPoints
-- Found SFML 2.5.1 in /usr/lib64/cmake/SFML
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bayram/Projects/PolygonPoints/build
Consolidate compiler generated dependencies of target PolygonPoints
[ 33%] Linking CXX executable PolygonPoints
/usr/bin/ld: CMakeFiles/PolygonPoints.dir/src/main.cpp.o: in function `main':
main.cpp:(.text+0x173): undefined reference to `Point::Point(sf::Vector2<int>, sf::Vector2<int>)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/PolygonPoints.dir/build.make:117: PolygonPoints] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/PolygonPoints.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
【问题讨论】:
-
你是否在你的 point.cpp 文件中实现了
Point::Point(sf::Vector2<int>, sf::Vector2<int>)函数。也许你有一个错字。 -
在不使用 CMake 的情况下构建项目时是否收到相同的错误消息?
-
@drescherjm 是的,我确实实现了。
-
我会删除这个问题并提出一个新问题,其中包含一个 minimal reproducible example 没有 SFML 和 CMake。已经有一个答案和一些与 CMake 相关的 cmets。编辑会令人困惑。
标签: c++ cmake header-files sfml