Another CLion and SDL2 post
Hello,
I've read and tried all previous posts both here and elsewhere but all is in vain. Nothing is fixing my problem. I'm trying to get SDL2 to work with CLion in Windows 7. It worked about 6 months ago, but only recently picked up my project again. The only change initially was upgrading to the new CLion version.
Here is what I have:
CMakeLists:
project(SDL_Demo)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_Demo main.cpp)
target_link_libraries(SDL_Demo ${SDL2_LIBRARY} )
FindSDL2.cmake:
SET(SDL2_INCLUDE_DIR "C:\\sdl_lib\\SDL2-2.0.5\\x86_64-w64-mingw32\\include\\SDL2")
SET(SDL2_LIBRARY "C:\\sdl_lib\\SDL2-2.0.5\\x86_64-w64-mingw32\\lib")
SET(SDL2MAIN_LIBRARY "C:\\sdl_lib\\SDL2-2.0.5\\x86_64-w64-mingw32\\lib")
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
main.cpp:
#include <SDL.h>
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "hello" << std::endl;
return 0;
}
My error is:
"C:\Program Files\JetBrains\CLion 2017.3.1\bin\cmake\bin\cmake.exe" --build C:\Users\valgor\CLionProjects\test\cmake-build-debug --target SDL_Demo -- -j 2
Scanning dependencies of target SDL_Demo
[ 50%] Building CXX object CMakeFiles/SDL_Demo.dir/main.cpp.obj
[100%] Linking CXX executable SDL_Demo.exe
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_Demo.dir\build.make:96: recipe for target 'SDL_Demo.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/SDL_Demo.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/SDL_Demo.dir/rule' failed
Makefile:117: recipe for target 'SDL_Demo' failed
mingw32-make.exe[3]: *** [SDL_Demo.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_Demo.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_Demo.dir/rule] Error 2
mingw32-make.exe: *** [SDL_Demo] Error 2
My FindSDL2.cmake has hard coded paths at the moment, but I've tried the github script suggested in the other thread.
I've tried putting my SDL2 files in my project and outside of my project.
Another thread said this error was due to the order of -lSDL2main and -lSDL2 but I don't see how to set those here.
I've tried switch around both 32 and 64 bit versions.
Any thoughts? Thanks.
请先登录再写评论。
@Josh Baldwin 42 thanks for the post!
That's what I've done for testing on my side:
1) Downloaded SDL 2.0.7 Development Libraries (SDL2-devel-2.0.7-mingw.tar.gz (MinGW 32/64-bit)) from here.
2) Downloaded FindSDL2.cmake from here.
3) Put FindSDL2.cmake into the "Modules" CMake subfolder. It depends on what CMake you use. In my case (CLion 2017.3.1, bundled CMake) it's <CLion_istallation_path>\bin\cmake\share\cmake-3.9\Modules.
4) Created a new "Hello, World!" project in CLion.
5) Changed CMakeLists.txt so that it looks as follows:
6) Replaced the code in main.cpp with the one that you provided.
The project was built and linked fine.
I kindly ask you to do exactly the same, step by step. If after that you still get the same error message, please send your project to clion-support at jetbrains.com.
@Josh Baldwin 42 BTW I used MinGW w64 5.0 toolchain (File | Settings | Build, Execution, Deployment | Toolchains).
@Anna Falevskaya
HI, I did exactly what you said but when compiling, this is what I get, any idea how to fix it.:
@20140585
Hi. Do you use the bundled CMake or another? Have you put FindSDL2.cmake into Cmake' "Modules" directory (in case of bundled CMake it is <CLion_istallation_path>\bin\cmake\share\cmake-3.9\Modules)?
@Anna Falevskaya
Okay I solved it by adding
after putting the FindSDL2.cmake into the folder cmake.
Anyway, still huge problems. That is everything is red. I got like 10 trillion of the error "undefined reference". I was using codeblock, then switched to Clion. My project worked fine with code block but apparently Clion still cannot make out my SDL library. Even include SDL.h is red.
After the actions described in my comment everything worked fine and SDL.h wasn't red on my side, so CLion is capable of working with SDL library.
What version of CLion do you use? On which OS? What SDL distribution have you installed?
Please reload the CMake project (Tools | CMake | Reset Cache and Reload Project) and show the full output that you get in the CMake tool window. Also show your CMakeLists.txt.
@Anna Falevskaya
Yes Reset Cache helped to get rid of the red text. However, when compiling, I still get the undefined reference:
A bit of Google told me that my target_link_libraries got some issue, I think. I will just put the whole CMakeList here, in case you see something wrong but I didnt :<
@Anna Falevskaya
So what Toolchain environment are you using. I am using Cywind64, which maybe the cause of the problem I dunno. What are u using?
Yeah after 2 days, I have managed to make it work. I will just put my method. I am sure your works as well, on your PC. Did not work on mine for some reason. So here is an alternative way for people having trouble can fix this in the future.
1. MAKE SURE you are using MinGW 32 bit compiler. I used Cywind64...straight out did not work.
2. Download that FindSDL2.cmake file, and put in into a folder Cmake in the project directory.
3. Edit that FindSDL2.cmake file, the line right bellow a bunch of comments.
From
To
4. Download the SDL2-devel-2.0.7-mingw.tar.gz (Development Libraries), get the folder i686-w64-mingw32 and put it into Project Directory
5. Cmakelists.txt file:
6. Make sure to reload the Cmakelists.txt if you have not.
7. Place the SDL.dll(in i686-w64-mingw32\bin) into the folder cmake-build-debug created by Clion
8. Compile. Everything should work now.
Hello,
I am trying to setup SDL2 on Clion as well, I have followed those steps, and get the following error (using x86_64-w64-mingw32 and the x86_64-w64-mingw32 version of SDL2):
which is the same as you got here, however even following his steps I did not manage to fix it.
My CMakeLists.txt:
and main.cpp:
Any idea what is the issue here? Unfortunately I am learning but not well versed in Cmake, and have 2 left hands when it comes to toolchains, I might be missing the obvious.
Many thanks! Florian
Florian Poinsignon are you sure you put FindSDL2.cmake into "C:\Program Files\ JetBrains\CLion 2020.2.1\bin\cmake\win\share\cmake-3.17\Modules"?
Hi Anna,
Yes I put it in this directory! put a screenshot in case I overlooked something
Hi Anna,
Thanks for the reply by the way! I forgot earlier. I checked again because I thought there was a good reason you mentioned it and went to check again, as seen in the screenshot I missed the 4th from the last \win directory, thanks again for that! It seems to work now!
Florian Poinsignon just in case this "Modules" directory should already exist on the machine (in your case it should be "C:\Program Files\ JetBrains\CLion 2020.2.1\bin\cmake\win\share\cmake-3.17\Modules", I think), you shouldn't create it on your own.
I'm glad it works now!
Hey I tried your solution Anna and it didn't work.
I have FindSDL2.cmake here : C:\Program Files\JetBrains\CLion 2020.3.1\bin\cmake\win\share\cmake-3.17\Modules
Here is my CMakeLists.txt
here is my main.c
And here is what I get. Can you help me?
Edit : I got help from the discord server and figured it out :
I kept everything from the Anna solution but I had to do a few more things :
delete all the SDL2 files I had in C:/MinGW/lib/
add SDL2.dll file to cmake-build-debug folder (the same directory as the .exe file)
and it worked for me.
here is the explanation of what happened to me.
If you want to read the whole discussion join the discord (https://discord.gg/jetbrains) get the Clion role and follow this link : https://discord.com/channels/433980600391696384/434016056957206529/794965298087329813
I downloaded SDL2 tar.gz and FindSDL2.cmake and arranged in CMakeList.txt file. Project is built successfully there is no error while building the project but project ignore the SDL2/SDL.h. I am typing SDL but there is nothing to see.
I extracted the tar.gz winRAR into the users folder and got from it, and also added FindSDL2.cmake file in the Modules file in CLion which is in the Program Files.
Is there a point I missed? Could you help me?
Hi ZgrZkn!
message("${SDL2_INCLUDE_DIR}")
to the end of the CMakeLists.txt file, do `Tools | CMake | Reset Cache and Reload Project`, and share the entire output from the CMake tool window.I got the files out of onedrive. Fİrst of all, it is fixed but after that, I got "undefined SDL_main" error. Then, I delete the project by making some mistakes and create another project out of OneDrive. But again, <SDL.h> does not seem again.
ZgrZkn in this project created out of OneDrive, please add
message("${SDL2_INCLUDE_DIR}")
(mind one pair of curly brackets, there was a typo in my previous comment) to the end of the CMakeLists.txt file, do `Tools | CMake | Reset Cache and Reload Project`, and share the entire output from the CMake tool window. I need this output to investigate the issue.ZgrZkn that's not what I asked.
Please do `Tools | CMake | Reset Cache and Reload Project`, and share the entire output from the CMake tool window as text.
"C:\Program Files\JetBrains\CLion 2023.1.2\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2023.1.2/bin/ninja/win/x64/ninja.exe" -G Ninja -S C:\Users\ozgur\GitHub\SDL_ParticleFireSimulation -B C:\Users\ozgur\GitHub\SDL_ParticleFireSimulation\cmake-build-debug
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/JetBrains/CLion 2023.1.2/bin/mingw/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/JetBrains/CLion 2023.1.2/bin/mingw/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Found SDL2: mingw32;-mwindows;C:/MinGW/lib/libSDL2main.a;C:/MinGW/lib/libSDL2.dll.a
C:/MinGW/include/include/SDL2
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/ozgur/GitHub/SDL_ParticleFireSimulation/cmake-build-debug
[Finished]
Could you give an answer please ?
ZgrZkn As you can see from the output
that CMake found the SDL2 installation, but the different one: not from "C:\Users\ozgur\SDL2-2.28.1\x86_64-w64-mingw32", but from "C:\MinGW". And `SDL2_INCLUDE_DIR` is set to "C:\MinGW\include\include\SDL2".
It looks like you're compiling a single file. Instead please use `Build | Build Project` or `Build | Build 'SDL_ParticleFireSimulation'` from the main menu.
I built the project by using this section.
After building, I gave this error :
SDL_ParticleFireSimulation.exe
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE -g CMakeFiles/SDL_ParticleFireSimulation.dir/main.cpp.obj -o SDL_ParticleFireSimulation.exe -Wl,--out-implib,libSDL_ParticleFireSimulation.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lmingw32 -mwindows C:/MinGW/lib/libSDL2main.a C:/MinGW/lib/libSDL2.dll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2023.1.2\bin\mingw\bin/ld.exe: C:/MinGW/lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.28.1-source/foo-x64/../src/main/windows/SDL_windows_main.c:80: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
I deleted the SDL2 file from MinGW and have reset the CMake and gave me the solution
-- Found SDL2: mingw32;-mwindows;C:/MinGW/lib/libSDL2main.a;C:/MinGW/lib/libSDL2.dll.a
C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/include/SDL2
But still, SDL.h does not seem. Cause project is not build.
ZgrZkn please do the following:
After that do you have the Build 'SDL_ParticleFireSimulation' option in the Build menu?
I can use SDL.h library now, But still I cannot build the project. When I tried to build project, I got this error :
FAILED: SDL_ParticleFireSimulation.exe
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE -g CMakeFiles/SDL_ParticleFireSimulation.dir/main.cpp.obj -o SDL_ParticleFireSimulation.exe -Wl,--out-implib,libSDL_ParticleFireSimulation.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lmingw32 -mwindows C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/lib/libSDL2main.a C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/lib/libSDL2.dll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2023.1.2\bin\mingw\bin/ld.exe: C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.28.1-source/foo-x64/../src/main/windows/SDL_windows_main.c:80: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ZgrZkn please try to put the arguments to the main function as suggested here https://stackoverflow.com/a/36120759.
When I tried before, it was returning 0 but not giving the output. This time I'm getting a reference error.
I think about that maybe my SDL has a problem but I installed SDL that you recommended.