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.

 

1

@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\\x86_64-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

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

1

@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

@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

@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

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

@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

@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

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

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

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

Hi Anna,

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

0

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

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

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

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

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?

0

Hi ZgrZkn!

  1. Where exactly did you place FindSDL2.cmake? 
  2. Please add 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.
  3. Does it work if you place the project not in the OneDrive directory, but in the home directory (C:\Users\ozgur), for example?
0

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.

0

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.

0

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. 

0

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

0

Could you give an answer please ?

0

ZgrZkn As you can see from the output

Found SDL2: mingw32;-mwindows;C:/MinGW/lib/libSDL2main.a;C:/MinGW/lib/libSDL2.dll.a  
C:/MinGW/include/include/SDL2

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

  1. Does SDL.h actually exist in "C:\MinGW\include\include\SDL2"?
  2. How exactly do you build the project in CLion? What button do you click?

It looks like you're compiling a single file. Instead please use `Build | Build Project` or `Build | Build 'SDL_ParticleFireSimulation'` from the main menu.

0

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.

0

ZgrZkn please do the following:

  1. Close the project (`File | Close Project`),
  2. Delete the `.idea` subfolder from `C:/Users/ozgur/GitHub/SDL_ParticleFireSimulation`,
  3. On CLion Welcome screen choose `Open` and select `C:/Users/ozgur/GitHub/SDL_ParticleFireSimulation`,
  4. Wait till the CMake project is loaded.

After that do you have the Build 'SDL_ParticleFireSimulation' option in the Build menu?

 

0

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.

0

ZgrZkn please try to put the arguments to the main function as suggested here https://stackoverflow.com/a/36120759.

0


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. 

0

请先登录再写评论。