Search code examples
c++cmakeparameter-passingsfmlclion

Compiling C++ with parameters using CMake in CLion throws errors while compiling in terminal does not


I've got a simple code which uses SFML library and it requires to be compiled with

-lsfml-graphics -lsfml-system -lsfml-window parameters.

It works just fine when I simply use cmd

  g++ gejm.cpp -o gejm -lsfml-graphics -lsfml-system -lsfml-window

  .\gejm.exe

However when I run it using CLion I get errors as if my parameters weren't passed. But I know my parameters ARE passed because if I misspell any of them an error is thrown before my code even gets compiled.

My CMakeLists.txt looks like this

set(GCC_COVERAGE_COMPILE_FLAGS "-lsfml-graphics -lsfml-system -lsfml-window")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )

add_executable(gejm gejm.cpp)

So what is the problem?

...here's the errors

CMakeFiles\gejm.dir/objects.a(gejm.obj): In function `main':
D:/Projects/CPP/GUI/gejm.cpp:8: undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
D:/Projects/CPP/GUI/gejm.cpp:8: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
D:/Projects/CPP/GUI/gejm.cpp:8: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
D:/Projects/CPP/GUI/gejm.cpp:9: undefined reference to `_imp___ZNK2sf10WindowBase6isOpenEv'
D:/Projects/CPP/GUI/gejm.cpp:11: undefined reference to `_imp___ZN2sf10WindowBase9pollEventERNS_5EventE'
D:/Projects/CPP/GUI/gejm.cpp:13: undefined reference to `_imp___ZN2sf6Window5closeEv'
D:/Projects/CPP/GUI/gejm.cpp:17: undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
D:/Projects/CPP/GUI/gejm.cpp:17: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
D:/Projects/CPP/GUI/gejm.cpp:18: undefined reference to `_imp___ZN2sf6Window7displayEv'
D:/Projects/CPP/GUI/gejm.cpp:8: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
D:/Projects/CPP/GUI/gejm.cpp:8: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\gejm.dir\build.make:84: recipe for target 'gejm.exe' failed
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/gejm.dir/all' failed
mingw32-make.exe[3]: *** [gejm.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/gejm.dir/all] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/gejm.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/gejm.dir/rule] Error 2
mingw32-make.exe: *** [gejm] Error 2
Makefile:117: recipe for target 'gejm' failed

Solution

  • This is not how to add libraries:

    set(GCC_COVERAGE_COMPILE_FLAGS "-lsfml-graphics -lsfml-system -lsfml-window")
    

    Add these libraries to the list of libraries:

    target_link_libraries(gejm PUBLIC sfml-graphics sfml-system sfml-window)
    

    Th reason it doesn't work is that the link stage is not the compilation stage and you only overrode the compilation flags, not the link flags. Anyway, it would not have been the proper way to go. Use what CMake provides, don't hack something in.