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.

 

1
47 comments

Copy SDL libraries from `C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/lib` to the `cmake-build-debug` subfolder of your project.

0

It did not fix ?

0

Not the "lib" folder, but the libraries themselves.

0

Like this ? If is this what you said, it did not work again. If it is not, could you explain more clearly please ? 

0

Yes, that's what I meant by copying the SDL2 libraries to the "cmake-build-debug" folder.

Please delete the entire "C:/MinGW/include/include/SDL2" directory and all the SDL2-related libraries (like libSDL2main.a, libSDL2.dll.a, etc.) from the "C:/MinGW/lib" directory.

After that once again do `Tools | CMake | Reset Cache and Reload Project` and show the entire output from the CMake tool window as text.

0

Hello Anna, unfortunately I could not solve the problems. I also deleted CLion and also deleted the SDL files. Then I reinstalled CLion. I followed your suggestions but there are some differences. The Modules part is according to the path you gave

<CLion_istallation_path>\bin\cmake\share\cmake-3.9\Modules

but I have the Modules part

<CLion_istallation_path>\bin\cmake\cygwin\x64\share\cmake-3.25.2\Modules.

Does it create a problem? I put the FindSDL2.cmake file here. Afterwards, the SDL2 file did not appear for me and I was advised by the CLion compiler to download vcpkg via a button named [fix] in the error, so I downloaded it and the SDL file appeared. But when building the project:

 

cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.5\bin\mingw\bin\G__~1.EXE -g CMakeFiles/untitled.dir/main.cpp.obj -o untitled.exe -Wl,--out-implib,libuntitled,-dll. kernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cmd.exe /C "cd /D C:\Users\ozgur\CLionProjects\untitled\cmake-build-debug && C:\Windowsproshellfile\passcule C:\Windowsprollcy.32\Windows :/Users/ozgur/.clion-vcpkg/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug/untitled.exe -installedDir C:/Users/Osers/kgpows/installx64kgpbugs/c ariable out"" C:\Program Files\JetBrains\CLion 2023.1.5\bin\mingw\bin/ld.exe: C:/PROGRA~1/JETBRA~1/CLION2~1.5/bin/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../..64x../32-lib../. (lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text+0x46): undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed.

 

I get this error. After that, I added #define SDL_MAIN_HANDLED this definition, and work the code. But this time, I tried to run an SDL function, Then :

 

FAILED: untitled.exe 
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.5\bin\mingw\bin\G__~1.EXE -g  CMakeFiles/untitled.dir/main.cpp.obj -o untitled.exe -Wl,--out-implib,libuntitled.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cmd.exe /C "cd /D C:\Users\ozgur\CLionProjects\untitled\cmake-build-debug && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file C:/Users/ozgur/.clion-vcpkg/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug/untitled.exe -installedDir C:/Users/ozgur/.clion-vcpkg/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
C:\Program Files\JetBrains\CLion 2023.1.5\bin\mingw\bin/ld.exe: CMakeFiles/untitled.dir/main.cpp.obj:C:/Users/ozgur/CLionProjects/untitled/main.cpp:7: undefined reference to `SDL_Init'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I got this error.

 
 
 
0

ZgrZkn I won't be able to help you if you continue not to follow my advice and do something completely different. 

<CLion_istallation_path>\bin\cmake\cygwin\x64\share\cmake-3.25.2\Modules

As you can see from the path, this "Modules" directory is related to CMake from the Cygwin toolchain. And you're using the MinGW toolchain. That's why the FindSDL2.cmake file isn't used by CMake from the MinGW toolchain if you put it in <CLion_istallation_path>\bin\cmake\cygwin\x64\share\cmake-3.25.2\Modules.

The proper path in your case would be <CLion_istallation_path>\bin\cmake\win\x64\share\cmake-3.25.2\Modules.

As for vcpkg: you might have installed the incorrect triplet (version of the SDL2 package).

Please just answer the following questions and don't do anything other than that:

  1. Do you still have the "C:\Users\ozgur\SDL2-2.28.1\x86_64-w64-mingw32" directory?
  2. Have you removed the entire "C:/MinGW/include/include/SDL2" directory and all the SDL2-related libraries from the "C:/MinGW/lib" directory?
  3. Please show a screenshot of `C:\Users\ozgur\.clion-vcpkg\vcpkg\packages`.
  4. Share your current CMakeLists.txt.
  5. Do `Tools | CMake | Reset Cache and Reload Project` and show the entire output from the CMake tool window as text.
0

That is the cmake folder in CLion version which I used already. Why it is completely different.
A1 :

A2 : Yes I deleted, I searched all sdl libraries from Search section and I did not find any folder or library.

In MinGW/lib

A3 : 

A4:

A5: 

"C:\Program Files\JetBrains\CLion 2023.1.5\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2023.1.5/bin/ninja/win/x64/ninja.exe" -DCMAKE_TOOLCHAIN_FILE=C:\Users\ozgur\.clion-vcpkg\vcpkg\scripts\buildsystems\vcpkg.cmake -G Ninja -S C:\Users\ozgur\CLionProjects\untitled -B C:\Users\ozgur\CLionProjects\untitled\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.5/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.5/bin/mingw/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
C:/Users/ozgur/.clion-vcpkg/vcpkg/installed/x64-windows/include/SDL2
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug

[Finished]

 

0

Please do exactly the following:

  1. Place the FindSDL2.cmake file into "C:\Program Files\JetBrains\CLion 2023.1.5\bin\cmake\win\x64\share\cmake-3.25.2\Modules" (please pay attention to the path!).
  2. Delete the "C:\Users\ozgur\.clion-vcpkg" directory.
  3. In CLion open the vcpkg tool window (View | Tool Windows | Vcpkg), select the vcpkg installation and click the "Delete" icon
  4. In CLion go to `File | Settings | Build, Execution, Deployment | CMake` and empty the "CMake options" field, click `OK`.
  5. Add message("${SDL2_LIBRARY}") 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.
  6. Empty the main.cpp file and place there the following code:
#include <SDL.h>
#include <iostream>

int main(int argc, char *argv[]) {
std::cout << "hello" << std::endl;
return 0;
}

6. Build the project (`Build | Build Project`) and share the entire output from the Messages tool window.

0

A1:

In folder cmake-3.25.2 looks like cmake-3-25. It does not have .2. But I put the FindSDL2.cmake folder in it nevertheless.

A2:

I deleted all .clion-vcpkg folder.

A3:

I deleted already whole vcpkg instollation from CLion.

A4 :

My CMake options look like this. I click on it and it looks empty. So I clicked OK button. I hope that is what I need to do.

A5 : 

"C:\Program Files\JetBrains\CLion 2023.1.5\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2023.1.5/bin/ninja/win/x64/ninja.exe" -G Ninja -S C:\Users\ozgur\CLionProjects\untitled -B C:\Users\ozgur\CLionProjects\untitled\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.5/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.5/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:/Users/ozgur/CLionProjects/untitled/cmake-build-debug/libSDL2main.a;C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug/libSDL2.dll.a  
C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/include/SDL2
mingw32;-mwindows;C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug/libSDL2main.a;C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug/libSDL2.dll.a
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/ozgur/CLionProjects/untitled/cmake-build-debug

[Finished]

 

A6 : 

"C:\Program Files\JetBrains\CLion 2023.1.5\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\ozgur\CLionProjects\untitled\cmake-build-debug --target all -j 10
[1/2] Building CXX object CMakeFiles/untitled.dir/main.cpp.obj
[2/2] Linking CXX executable untitled.exe

Build finished

But I am getting this output when I try to run the code : 

C:\Users\ozgur\CLionProjects\untitled\cmake-build-debug\untitled.exe

Process finished with exit code -1073741515 (0xC0000135)

0

ZgrZkn as far as I can see, the project was compiled successfully.

Are you able to run the project?

0

I can run the project . That is the output. 

When I clicked Run button I am getting this output on the console.

Does Microsoft Defender cause this problem ? 

0

ZgrZkn I reproduced the issue on my side. And I was able to fix it in the following way:

  1. Remove the SDL2-related libraries from the "cmake-build-debug" directory.
  2. Copy the SDL.dll library from `C:/Users/ozgur/SDL2-2.28.1/x86_64-w64-mingw32/bin` to the `cmake-build-debug` directory.
  3. Try running the project.

Does the process finish with exit code 0 (which means that it runs without errors)? It does on my side:

0

Anna, thank you very, very much. Thank you for answering all the problems without getting bored (sometimes I may have got what you said wrong). I wish I could hug you to thank you for your efforts. You saved me from so much trouble. Words are not enough to praise you. But I can't see hello, I hope it's normal because I don't want to deal with errors anymore and I don't want to bother you. :/

0

ZgrZkn I'm glad it finally works for you! And thank you for your kind words!

But I can't see hello

As far as I know, this is the specifics of the SDL2 library. There are lots of discussions on this topic on the internet. Here are a couple of examples: https://www.gamedev.net/forums/topic/478009-cout-with-sdl/4133230/, https://stackoverflow.com/q/34539135.

BTW you should be able to see "hello" if you use Debug instead of Run:

Unfortunately, I can't explain why SDL program behaves this way since I'm not a specialist in SDL.

 

0

Thank you so much Anna, I don't know what I would do without you. I'm so glad to have you. <3

0

Please sign in to leave a comment.