Reputation: 17
So i am really new to windows environment and i am trying to understand how to work with c++ libraries and cMake. My first goal was to convert a linux project that I was developping in linux to windows by changing cMake and downloading mysys to download packages. I manage to do it in 2 days... (I was using glfw and glm in my project) From what I learned I wanted to create a new project using sfml libraries. So I downloaded packages using msys2 I set my toolset to C:\msys64\mingw64 and downloaded packages using this site: https://packages.msys2.org/package/mingw-w64-x86_64-sfml?repo=mingw64 I canged my cMake as following:
cmake_minimum_required(VERSION 3.22)
project(Pong)
set(CMAKE_CXX_STANDARD 14)
find_package(SFML 2.5 COMPONENTS network graphics audio window system REQUIRED)
add_executable(Pong main.cpp ${SFML_LIBRARIES})
but it gives me this error:
FAILED: Pong.exe
cmd.exe /C "cd . && C:\msys64\mingw64\bin\c++.exe -g CMakeFiles/Pong.dir/main.cpp.obj -o Pong.exe -Wl,--out-implib,libPong.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/Pong.dir/main.cpp.obj:C:/Users/tanku/Documents/Projects/Pong/main.cpp:5: undefined reference to `__imp__ZN2sf6StringC1EPKcRKSt6locale'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/Pong.dir/main.cpp.obj: in function `main':
C:/Users/tanku/Documents/Projects/Pong/main.cpp:5: undefined reference to `__imp__ZN2sf9VideoModeC1Ejjj'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:5: undefined reference to `__imp__ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:6: undefined reference to `__imp__ZN2sf11CircleShapeC1Efy'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:7: undefined reference to `__imp__ZN2sf5Color5GreenE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:7: undefined reference to `__imp__ZN2sf5Shape12setFillColorERKNS_5ColorE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:15: undefined reference to `__imp__ZN2sf6Window5closeEv'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:12: undefined reference to `__imp__ZN2sf6Window9pollEventERNS_5EventE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:18: undefined reference to `__imp__ZN2sf5ColorC1Ehhhh'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:18: undefined reference to `__imp__ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:19: undefined reference to `__imp__ZN2sf12RenderStates7DefaultE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:19: undefined reference to `__imp__ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:20: undefined reference to `__imp__ZN2sf6Window7displayEv'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:9: undefined reference to `__imp__ZNK2sf6Window6isOpenEv'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:24: undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/tanku/Documents/Projects/Pong/main.cpp:24: undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/Pong.dir/main.cpp.obj: in function `sf::CircleShape::~CircleShape()':
C:/msys64/mingw64/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `__imp__ZTVN2sf11CircleShapeE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `__imp__ZTVN2sf11CircleShapeE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `__imp__ZN2sf5ShapeD2Ev'
After a quick research i saw that thats because I have to download DW2 version of SFML but I cant find that in msys2 packages there is only SFML packages that I installed.
My main is the simple example program from the site sfml:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
I had a similar error in my first project that I was talking about. The solution was to use packet manager and use find_package(..) instead of using hard codded paths.
Is it possible that I downloaded the wrong packages from msys2. If so how can I correct it?
Upvotes: 1
Views: 282
Reputation: 19966
This line is wrong:
add_executable(Pong main.cpp ${SFML_LIBRARIES})
First of all, the SFML libraries are not source files for Pong
. So you probably meant to write:
add_executable(Pong main.cpp)
target_link_libraries(Pong PRIVATE ${SFML_LIBRARIES})
But then this is still wrong because the SFML package does not define a variable named SFML_LIBRARIES
! Instead, SFML does the good and correct thing for modern CMake, which is to define imported targets. Whenever a package provides imported targets, you should link to them directly. In this case, you can write:
cmake_minimum_required(VERSION 3.22)
project(Pong)
find_package(SFML 2.5 COMPONENTS network graphics audio REQUIRED)
add_executable(Pong main.cpp)
target_compile_features(Pong PRIVATE cxx_std_14)
target_link_libraries(Pong PRIVATE sfml-network sfml-graphics sfml-audio)
This should now work. Since window
and system
are implied by graphics
they don't need to be listed explicitly. Finally, I'm using the more modern way of setting the required C++ standard version (target_compile_features
).
Also note that SFML's own SFMLConfig.cmake
file provides some documentation in the comments at the top. Look for the path mingw64/lib/cmake/SFML/SFMLConfig.cmake
on your system.
Upvotes: 2