Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] How to add new class files to CMakeLists  (Read 2371 times)

0 Members and 1 Guest are viewing this topic.

s4game

  • Newbie
  • *
  • Posts: 8
    • View Profile
[SOLVED] How to add new class files to CMakeLists
« on: June 16, 2024, 09:00:28 pm »
Hello,
I have been getting fails every time CMake builts the project:
Quote
[main] Building folder: sfml-cmake all
[build] Starting build
[proc] Executing command: D:\Prgrams\CMake\bin\cmake.EXE --build d:/VcProj/sfml-cmake/build --config Debug --target all --
[build] [1/2  50% :: 1.086] Building CXX object CMakeFiles/main.dir/src/main.cpp.obj
[build] [2/2 100% :: 2.083] Linking CXX executable bin\main.exe
[build] FAILED: bin/main.exe
[build] C:\WINDOWS\system32\cmd.exe /C "C:\WINDOWS\system32\cmd.exe /C "cd /D D:\VcProj\sfml-cmake\build && D:\Prgrams\CMake\bin\cmake.exe -E copy D:/VcProj/sfml-cmake/build/_deps/sfml-src/extlibs/bin/x64/openal32.dll D:/VcProj/sfml-cmake/build/bin && cd D:\VcProj\sfml-cmake\build" && C:\msys64\ucrt64\bin\c++.exe -g  CMakeFiles/main.dir/src/main.cpp.obj -o bin\main.exe -Wl,--out-implib,libmain.dll.a -Wl,--major-image-version,0,--minor-image-version,0  _deps/sfml-build/lib/libsfml-graphics-s-d.a  _deps/sfml-build/lib/libsfml-window-s-d.a  _deps/sfml-build/lib/libsfml-system-s-d.a  -lopengl32  -lwinmm  -lgdi32  _deps/sfml-src/extlibs/libs-mingw/x64/libfreetype.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
[build] C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/main.dir/src/main.cpp.obj: in function `main':
[build] D:/VcProj/sfml-cmake/src/main.cpp:9: undefined reference to `Game::update()'
[build] C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/VcProj/sfml-cmake/src/main.cpp:10: undefined reference to `Game::render()'
[build] C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/VcProj/sfml-cmake/src/main.cpp:7: undefined reference to `Game::isRunning()'
[build] collect2.exe: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: D:\Prgrams\CMake\bin\cmake.EXE --build d:/VcProj/sfml-cmake/build --config Debug --target all -- exited with code: 1
[driver] Build completed: 00:00:02.171
[build] Build finished with exit code 1

But I figure that since my code uses classes from external files, it can only result in a failed build. So the only problem I'm left with is how do I link new CPP and header files to the CMakeLists.txt?

For the reference, here are the contents of main.cpp:
Quote
#include "game.h"

int main()
{
    Game game;

    while(game.isRunning())
    {
        game.update();
        game.render();
    }
 
}



Configuration:
  • Windows 10
  • C++ MinGW 13.1.0
  • CMake template from github
  • VS Code

Thanks in advance
« Last Edit: June 18, 2024, 11:55:23 am by s4game »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to add new class files to CMakeLists
« Reply #1 on: June 17, 2024, 09:49:14 am »
As mentioned on step five: https://github.com/SFML/cmake-sfml-project?tab=readme-ov-file#how-to-use

Quote
If you want to add or remove any .cpp files, change the source files listed in the add_executable call in CMakeLists.txt to match the source files your project requires. If you plan on keeping the default main.cpp file then no changes are required.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

s4game

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How to add new class files to CMakeLists
« Reply #2 on: June 17, 2024, 10:54:35 am »
As far as I have tried to understand and try adding additional CPP and Header files, my attempts were unsuccessful. Could you perhaps show an example of CMakeLists.txt linked with some external file classes?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How to add new class files to CMakeLists
« Reply #3 on: June 17, 2024, 12:18:50 pm »
Sure, I've added a Game.cpp file below

cmake_minimum_required(VERSION 3.16)
project(CMakeSFMLProject LANGUAGES CXX)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

include(FetchContent)
FetchContent_Declare(SFML
    GIT_REPOSITORY https://github.com/SFML/SFML.git
    GIT_TAG 2.6.x)
FetchContent_MakeAvailable(SFML)

add_executable(main src/main.cpp src/Game.cpp)
target_link_libraries(main PRIVATE sfml-graphics)
target_compile_features(main PRIVATE cxx_std_17)

if(WIN32)
    add_custom_command(
        TARGET main
        COMMENT "Copy OpenAL DLL"
        PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:main>
        VERBATIM)
endif()
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

s4game

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How to add new class files to CMakeLists
« Reply #4 on: June 18, 2024, 11:55:11 am »
So no header file is needed to be linked? I've tried it and everything seems to work now. Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [SOLVED] How to add new class files to CMakeLists
« Reply #5 on: June 18, 2024, 03:48:29 pm »
You can list them, but they're not necessary.
If you don't listen them and use VS projects, they header files wouldn't necessarily show up as part of the project, but it will still compile just fine.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/