-1

Hello i am trying to build my SFML project using CMake but when i try to use multiple source files, CMake doesn't link the object files

This is my 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)

This is my project structure:

PolygonPoints/
    src/
        main.cpp
        point.cpp
    include/
        point.hpp
    build/
        (...)
    CMakeLists.txt
    (...)

This is the error I get:

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
bayramkazik
  • 27
  • 1
  • 9
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Thomas Sablik Oct 03 '21 at 13:24
  • 1
    Did you implement the `Point::Point(sf::Vector2, sf::Vector2)` function in your point.cpp file. Perhaps you have a typo. – drescherjm Oct 03 '21 at 13:26
  • Do you get the same error message when you build your project without CMake? – Thomas Sablik Oct 03 '21 at 13:28
  • @drescherjm Yes I did implement. – bayramkazik Oct 03 '21 at 13:30
  • You may have to look at the Makefile. This is odd and unexpected. – drescherjm Oct 03 '21 at 13:32
  • @ThomasSablik I tried compiling in the commandline with `g++ -I include/ -c src/*.cpp && g++ -lsfml-graphics -lsfml-window -lsfml-system main.o point.o` and I got the same error. Interesting I thought the problem was about CMake. – bayramkazik Oct 03 '21 at 13:48
  • In that case it's probably a duplicate. You can check the link in my first comment. – Thomas Sablik Oct 03 '21 at 14:02
  • There is **no implementation** of the given `Point` constructor in all your source files. This is what the error message is about. Something wrong with your `point.cpp`, but since you don't show it, we cannot help you in finding an error. – Tsyvarev Oct 03 '21 at 14:03
  • @Tsyvarev You are right sorry I was pretty sure the error had something to do with CMake so didn't attach the source code. After playing with the code more, I realized the code also doesn't have anything to do with SFML. At this point I am not sure whether I should create a new question with a code refined from SFML stuff or just edit this one. – bayramkazik Oct 03 '21 at 14:22
  • 1
    I would delete this question and ask a new question containing a [mcve] without SFML and CMake. There already is an answer and some comments related to CMake. An edit would be confusing. – Thomas Sablik Oct 03 '21 at 14:30
  • @ThomasSablik This seems reasonable. I am now deleting this question. – bayramkazik Oct 03 '21 at 14:31
  • Turns out I can't delete the question directly when there is an answer. I flagged the question to be deleted with an explanation. I hope mods delete it soon. – bayramkazik Oct 03 '21 at 14:41

1 Answers1

1

Be careful here:

file(
    GLOB
    SOURCES
    "src/*.hpp"
    "src/*.cpp"
)

As i understand it this is ran ONLY during cmake project configuration so if you have added a .cpp file later it will not be included with the build. You are much better to explicitly list your files in the executable as so:

add_executable(PolygonPoints 
    src/main.cpp
    src/point.cpp
)

When you add a file now you have to edit the add_executable which causes cmake to re-evaluate itself and regenerate the build/make files (depending on your generator) and thus files are always compiled as expected.

kanoisa
  • 390
  • 1
  • 5
  • 1
    It's clearly described in the comments that this problem is unrelated to CMake and also occurs without CMake. It's also unrelated to SFML. – Thomas Sablik Oct 03 '21 at 14:38
  • "@ThomasSablik I tried compiling in the commandline with g++ -I include/ -c src/*.cpp && g++ -lsfml-graphics -lsfml-window -lsfml-system main.o point.o and I got the same error. Interesting I thought the problem was about CMake" – Thomas Sablik Oct 03 '21 at 14:39