Another CLion and SDL2 post Follow
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.
Please sign in to leave a comment.
@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