Another CLion and SDL2 post

Answered

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.

 

16 comments
Comment actions Permalink

@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:

cmake_minimum_required(VERSION 3.9)
project(untitled2)
set(CMAKE_CXX_STANDARD 11)

set(SDL2_PATH "C:\\Users\\<user_name>\\Downloads\\SDL2-2.0.7\\i686-w64-mingw32")
find_package(SDL2 REQUIRED)
include_directories( ${SDL2_INCLUDE_DIR} )

add_executable(untitled2 main.cpp)

target_link_libraries(untitled2 ${SDL2_LIBRARY} )

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.

 

 

0
Comment actions Permalink

@Josh Baldwin 42 BTW I used MinGW w64 5.0 toolchain (File | Settings | Build, Execution, Deployment | Toolchains).

0
Comment actions Permalink

@Anna Falevskaya

HI, I did exactly what you said but when compiling, this is what I get, any idea how to fix it.:

CMake Error at CMakeLists.txt:9 (find_package):
By not providing "FindSDL2.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SDL2", but
CMake did not find one.

Could not find a package configuration file provided by "SDL2" with any of
the following names:

SDL2Config.cmake
sdl2-config.cmake

Add the installation prefix of "SDL2" to CMAKE_PREFIX_PATH or set
"SDL2_DIR" to a directory containing one of the above files. If "SDL2"
provides a separate development package or SDK, be sure it has been
installed.

0
Comment actions Permalink

@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)?

0
Comment actions Permalink

@Anna Falevskaya 

Okay I solved it by adding

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

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. 

0
Comment actions Permalink

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.

0
Comment actions Permalink

@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 :<

cmake_minimum_required(VERSION 3.9)
project(Battleship)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(SDL2_PATH ${PROJECT_SOURCE_DIR}/i686-w64-mingw32)

find_package(SDL2 REQUIRED)
include_directories( ${SDL2_INCLUDE_DIR} )


add_executable(Battleship
Battle.c
constant.h
Cruiser.c
Cruiser.h
Draw.c
Draw.h
Editor.c
Editor.h
EditorTexture.c
EditorTexture.h
getstates.c
handlemouseEditor.c
handlemouseEditor.h
main.c
Number.c
Number.h
ship.c
ship.h
specialbox.c
specialbox.h
states.c
states.h
submarine.c
submarine.h
Utils.c
Utils.h)

target_link_libraries(Battleship ${SDL2_LIBRARY})
0
Comment actions Permalink

@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?

0
Comment actions Permalink

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

SET(SDL2_SEARCH_PATHS
 
~/Library/Frameworks
 
/Library/Frameworks
 
/usr/local
 
/usr
 
/sw # Fink
 
/opt/local # DarwinPorts
 
/opt/csw # Blastwave
 
/opt
 
${SDL2_PATH}
)

To

set(SDL2_PATH "[YOUR PATH TO THE PROJECT DIRECTORY]\\i686-w64-mingw32")

SET(SDL2_SEARCH_PATHS ${SDL2_PATH}

 

 

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:

cmake_minimum_required(VERSION 3.9)
project(YOUR_PROJECT)

set(CMAKE_C_STANDARD 99)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
find_package(SDL2 REQUIRED)

add_executable(YOUR_PROJECT
main.c)

include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_MIXER_INCLUDE_DIR}
)
target_link_libraries(YOUR_PROJECT ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARY}
${SDL2_MIXER_LIBRARY}
)

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.

0
Comment actions Permalink

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:

cmake_minimum_required(VERSION 3.17)
project(Test_5)
set(CMAKE_CXX_STANDARD 14)

set(SDL2_PATH "C:\\Users\\Shizune\\Downloads\\SDL2-2.0.12\\x86_64-w64-mingw32")
#set(SDL2_PATH "C:\\Users\\Shizune\\Downloads\\SDL2-2.0.12\\i686-w64-mingw32")

find_package(SDL2 REQUIRED)
include_directories( ${SDL2_INCLUDE_DIR} )

add_executable(Test_5 main.cpp)

target_link_libraries(Test_5 ${SDL2_LIBRARY} )

and main.cpp:

#include <iostream>
#include <SDL.h>


int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}

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

0
Comment actions Permalink

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"?

0
Comment actions Permalink

Hi Anna,

Yes I put it in this directory! put a screenshot in case I overlooked something

0
Comment actions Permalink

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! 

 

0
Comment actions Permalink

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!

0
Comment actions Permalink

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

cmake_minimum_required(VERSION 3.9)
project(ImageOfCLife)
set(CMAKE_C_STANDARD 99)

set(SDL2_PATH "C:\\Users\\Clement\\Documents\\coding\\libraries\\SDL2-devel-2.0.14-mingw\\SDL2-2.0.14\\i686-w64-mingw32")
find_package(SDL2 REQUIRED)
include_directories( ${SDL2_INCLUDE_DIR} )

add_executable(ImageOfCLife main.c)

target_link_libraries(ImageOfCLife ${SDL2_LIBRARY})

here is my main.c

#include <stdio.h>
#include <SDL.h>

int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}

And here is what I get. Can you help me?

====================[ Build | ImageOfCLife | Debug ]============================
"C:\Program Files\JetBrains\CLion 2020.3.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\Clement\Documents\coding\ImageOfCLife\cmake-build-debug --target ImageOfCLife -- -j 6
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Clement/Documents/coding/ImageOfCLife/cmake-build-debug
Scanning dependencies of target ImageOfCLife
[ 50%] Building C object CMakeFiles/ImageOfCLife.dir/main.c.obj
[100%] Linking C executable ImageOfCLife.exe
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [ImageOfCLife.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/ImageOfCLife.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/ImageOfCLife.dir/rule] Error 2
mingw32-make.exe: *** [ImageOfCLife] Error 2
CMakeFiles\ImageOfCLife.dir\build.make:107: recipe for target 'ImageOfCLife.exe' failed
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/ImageOfCLife.dir/all' failed
CMakeFiles\Makefile2:101: recipe for target 'CMakeFiles/ImageOfCLife.dir/rule' failed
Makefile:137: recipe for target 'ImageOfCLife' failed
0
Comment actions Permalink

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

0

Please sign in to leave a comment.