Adding SDL2 to Clion - cmake

I am a beginner, I was trying to get SDL2 to work in clion but I failed.
I've searched on youtube and google but nothing worked. I have SDL2.dll in the same folder as main.cpp
My cmake file looks like this:
cmake_minimum_required(VERSION 3.8)
project(sdlTest)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp)
add_executable(sdlTest ${SOURCE_FILES})
add_library(SDL2.dll SHARED main.cpp)
set_target_properties(SDL2.dll PROPERTIES LINKER_LANGUAGE CXX)
I don't know what I'm doing wrong.
I tried running this example code from sdl:
#include "SDL2/SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: could enter program loop here (see SDL_PollEvent())
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
and I get this error:
CMakeFiles\sdlTest.dir/objects.a(main.cpp.obj): In function `SDL_main':
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:8: undefined reference to
`SDL_Init'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:18: undefined reference to
`SDL_CreateWindow'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:23: undefined reference to
`SDL_GetError'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:29: undefined reference to
`SDL_Delay'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:32: undefined reference to
`SDL_DestroyWindow'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:35: undefined reference to
`SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingw32.a(main.o):
(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [sdlTest.exe] Error 1
CMakeFiles\sdlTest.dir\build.make:95: recipe for target 'sdlTest.exe' failed
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/sdlTest.dir/all'
failed
CMakeFiles\Makefile2:115: recipe for target 'CMakeFiles/sdlTest.dir/rule'
failed
mingw32-make.exe[2]: *** [CMakeFiles/sdlTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/sdlTest.dir/rule] Error 2
mingw32-make.exe: *** [sdlTest] Error 2
Makefile:130: recipe for target 'sdlTest' failed
I've tried both x64 and x86 and it's the same result but at the same time clion doesn't red-text the code, it thinks it's ok:
Any ideas what I'm doing wrong and how to fix? THANKS!
EDIT:
Cmake looks like this
cmake_minimum_required(VERSION 3.8)
project(sdlTest)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(sdlTest ${SOURCE_FILES})
set(SDL2_LIBRARY SDL2.dll)
target_link_libraries(sdlTest ${SDL2_LIBRARYS})
I get this error:
Found package configuration file:
C:/MinGW/lib/cmake/SDL2/sdl2-config.cmake
but it set SDL2_FOUND to FALSE so package "SDL2" is considered to be NOT
FOUND.
SECOND EDIT:
sdl2-config.cmake:
# sdl2 cmake project-config input for ./configure scripts
set(prefix "/usr/local/x86_64-w64-mingw32")
set(exec_prefix "${prefix}")
set(libdir "${exec_prefix}/lib")
set(SDL2_PREFIX "/usr/local/x86_64-w64-mingw32")
set(SDL2_EXEC_PREFIX "/usr/local/x86_64-w64-mingw32")
set(SDL2_LIBDIR "${exec_prefix}/lib")
set(SDL2_INCLUDE_DIRS "${prefix}/include/SDL2")
set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} -lmingw32 -lSDL2main -lSDL2 -mwindows")
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)

Related

Process finished with exit code -1073741515 (0xC0000135) while using SDL2 in CLion [duplicate]

I've been trying to run OpenCV using CLion IDE under Windows. When I try to run this sample code for loading and displaying an image
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread("earth.jpg", CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I get the error statement:
Process finished with exit code -1073741515 (0xC0000135)
As for the content in my CMakeLists.txt, it looks like this:
cmake_minimum_required(VERSION 3.6)
project(test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "C:\\opencv\\mingw-build\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(openCV main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(openCV ${OpenCV_LIBS})
Thanks for helping me with this.
You need to add OpenCV binary path with DLLs to your PATH BEFORE CLion start.
I do it from script:
=== CLionWithMingwAndOpenCV.bat ==========================
#echo off
set PATH=C:\mingw-w64\x86_64-5.2.0-win32-seh-rt_v4-rev0\mingw64\bin;D:\opencv\release\bin;%PATH%
"C:\Program Files (x86)\JetBrains\CLion XXXX\bin\clion64.exe"
=== ==========================

How can i include SDL_gfx libraries in a Clion project?

So there is a weird thing that I've encountered, where i can make the SDL2 libraries work in clion but if i include the SDL_gfx libraries it won't compile.
So here is what I've done so far:
I've downloaded the Windows 64 version of SDL from the website, uncompressed it, and dragged the files in the corresponding MinGW folder.
I've edited the cmake file so that clion can see it as well, and it really works
My cmake file:
cmake_minimum_required(VERSION 3.15)
project(pontok C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
find_package(SDL2 REQUIRED)
add_executable(pontok main.c)
include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_MIXER_INCLUDE_DIR}
)
target_link_libraries(pontok ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARY}
${SDL2_MIXER_LIBRARY}
)
I've downloaded the SDL_gfx package from here and put the .h and .a files in the corresponding MinGW folders. Clion can see it, and it appears normal until I hit compile:
If my code is:
#include <stdio.h>
#include <math.h>
#include <SDL2\SDL2_gfxPrimitives.h>
#include <SDL2\SDL.h>
const int SCREEN_WIDH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
SDL_Log("Nem indithato az SDL: %s", SDL_GetError());
exit(1);
}
SDL_Window *window = SDL_CreateWindow("SDL peldaprogram", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 440, 360, 0);
if (window == NULL) {
SDL_Log("Nem hozhato letre az ablak: %s", SDL_GetError());
exit(1);
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == NULL) {
SDL_Log("Nem hozhato letre a megjelenito: %s", SDL_GetError());
exit(1);
}
SDL_RenderClear(renderer);
int x, y, r;
//circleRGBA(renderer, x, y, r, 255, 0, 0, 255);
//circleRGBA(renderer, x + r, y, r, 0, 255, 0, 255);
//circleRGBA(renderer, x + r * cos(3.1415 / 3), y - r * sin(3.1415 / 3), r, 0, 0, 255, 255);
SDL_RenderPresent(renderer);
SDL_Event ev;
while (SDL_WaitEvent(&ev) && ev.type != SDL_QUIT) {
}
SDL_Quit();
return 0;
}
Than it works. It compiles and the blank window appears. But if i remove those three lines from comments, then it wont compile, and I'll get the next error:
CMakeFiles\pontok.dir/objects.a(main.c.obj): In function `SDL_main':
C:/Prog/pontok/main.c:28: undefined reference to `circleRGBA'
C:/Prog/pontok/main.c:29: undefined reference to `circleRGBA'
C:/Prog/pontok/main.c:30: undefined reference to `circleRGBA'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\pontok.dir\build.make:88: pontok.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/pontok.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/pontok.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: pontok] Error 2
The weird thing is that clion can see these circleRGBA unctions, in this picture we can see that it can recognize the function, it writes the attributes next to the variables.
Unfortunately SDL_gfx does not have a standard cmake module. But you can find one on the internet by googling for FindSDL2_gfx.cmake. Put this file in your cmake/Modules directory.
find_package(SDL2_gfx REQUIRED)
Then use ${SDL2_GFX_LIBRARIES} for linking
Ok so i figured it out. The problem was that clion could not see the .c SDL2_gfx files, so i had to add them to the project and had to edit the following line in the CMakeList.txt:
add_executable(pontok main.c SDL2_framerate.c SDL2_gfxPrimitives.c SDL2_imageFilter.c SDL2_rotozoom.c)
This way it compiled and worked properly.

Linking a C++ exe to a C lib in cmake

I thought I understood cmake pretty well until I came accross this problem that I just can't figure out. I've built a static library in C, and I'm trying to run a unit test on it in C++, but I can't seem to link to any of the static functions in that library and I can't figure out why. I've reproduced the problem in the skeleton project below:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(myproj)
add_library(mylib STATIC mylib.c)
find_package(Boost 1.64.0 COMPONENTS unit_test_framework)
add_executable(mytestapp mytest.cpp)
target_include_directories(mytestapp PRIVATE .)
target_link_libraries(mytestapp mylib)
enable_testing()
add_test( mytest mytestapp)
mylib.c:
int add(int a, int b)
{
return a + b;
}
mylib.h
int add(int a, int b);
mytest.cpp:
#define BOOST_TEST_MODULE mylib_test
#include <boost/test/included/unit_test.hpp>
#include "mylib.h"
BOOST_AUTO_TEST_CASE(mylib_test)
{
BOOST_TEST( add(2,2) == 4);
}
Then my output is:
$ make
Scanning dependencies of target mylib
[ 25%] Building C object CMakeFiles/mylib.dir/mylib.c.o
[ 50%] Linking C static library libmylib.a
[ 50%] Built target mylib
Scanning dependencies of target mytestapp
[ 75%] Building CXX object CMakeFiles/mytestapp.dir/mytest.cpp.o
[100%] Linking CXX executable mytestapp
CMakeFiles/mytestapp.dir/mytest.cpp.o: In function `mylib_test::test_method()':
mytest.cpp:(.text+0x1e411): undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/mytestapp.dir/build.make:96: mytestapp] Error 1
make[1]: *** [CMakeFiles/Makefile2:105: CMakeFiles/mytestapp.dir/all] Error 2
make: *** [Makefile:95: all] Error 2
If I compile mylib in C++, then it links fine, but not in C. That's a problem for me because I have a huge library in C and I'm trying to use the boost_test_framework (in C++) to load this lib and test it.
Solution:
I needed to import the library with extern "C" like so:
#define BOOST_TEST_MODULE mylib_test
#include <boost/test/included/unit_test.hpp>
extern "C" {
#include "mylib.h"
}
BOOST_AUTO_TEST_CASE(mylib_test)
{
BOOST_TEST( add(2,2) == 4);
}

How to build dash with CMake?

I'm trying to build dash using Clion and CMake but I get this build error:
/opt/clion-2016.1.1/bin/cmake/bin/cmake --build /home/dac/.CLion2016.1/system/cmake/generated/dash-46b33cad/46b33cad/Debug --target all -- -j 4
[ 3%] Building C object CMakeFiles/main.dir/error.c.o
[ 6%] Building C object CMakeFiles/main.dir/eval.c.o
[ 9%] Building C object CMakeFiles/main.dir/cd.c.o
[ 12%] Building C object CMakeFiles/main.dir/arith_yylex.c.o
In file included from /home/dac/Downloads/dash-0.5.8/dash/error.c:54:0:
/home/dac/Downloads/dash-0.5.8/dash/system.h:65:22: error: static declaration of ‘strtod’ follows non-static declaration
static inline double strtod(const char *nptr, char **endptr)
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/error.c:40:0:
/usr/include/stdlib.h:164:15: note: previous declaration of ‘strtod’ was here
extern double strtod (const char *__restrict __nptr,
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/error.c:54:0:
/home/dac/Downloads/dash-0.5.8/dash/system.h:86:19: error: static declaration of ‘killpg’ follows non-static declaration
static inline int killpg(pid_t pid, int signal)
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/error.c:39:0:
/usr/include/signal.h:134:12: note: previous declaration of ‘killpg’ was here
extern int killpg (__pid_t __pgrp, int __sig) __THROW;
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/error.c:54:0:
/home/dac/Downloads/dash-0.5.8/dash/system.h:97:0: warning: "_SC_CLK_TCK" redefined
#define _SC_CLK_TCK 2
^
In file included from /usr/include/unistd.h:609:0,
from /home/dac/Downloads/dash-0.5.8/dash/error.c:42:
/usr/include/x86_64-linux-gnu/bits/confname.h:78:0: note: this is the location of the previous definition
#define _SC_CLK_TCK _SC_CLK_TCK
^
CMakeFiles/main.dir/build.make:182: recipe for target 'CMakeFiles/main.dir/error.c.o' failed
make[2]: *** [CMakeFiles/main.dir/error.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
/home/dac/Downloads/dash-0.5.8/dash/eval.c:51:22: fatal error: builtins.h: No such file or directory
In file included from /home/dac/Downloads/dash-0.5.8/dash/arith_yylex.c:44:0:
/home/dac/Downloads/dash-0.5.8/dash/system.h:65:22: error: static declaration of ‘strtod’ follows non-static declaration
static inline double strtod(const char *nptr, char **endptr)
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/arith_yylex.c:36:0:
/usr/include/stdlib.h:164:15: note: previous declaration of ‘strtod’ was here
extern double strtod (const char *__restrict __nptr,
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/arith_yylex.c:44:0:
/home/dac/Downloads/dash-0.5.8/dash/system.h:86:19: error: static declaration of ‘killpg’ follows non-static declaration
static inline int killpg(pid_t pid, int signal)
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/error.h:38:0,
from /home/dac/Downloads/dash-0.5.8/dash/arith_yylex.c:40:
/usr/include/signal.h:134:12: note: previous declaration of ‘killpg’ was here
extern int killpg (__pid_t __pgrp, int __sig) __THROW;
^
In file included from /home/dac/Downloads/dash-0.5.8/dash/syntax.h:5:0,
from /home/dac/Downloads/dash-0.5.8/dash/arith_yylex.c:43:
/home/dac/Downloads/dash-0.5.8/dash/system.h:102:17: error: expected ‘)’ before ‘c’
int isblank(int c);
^
/home/dac/Downloads/dash-0.5.8/dash/system.h:102:5: error: expected expression before ‘)’ token
int isblank(int c);
^
compilation terminated.
CMakeFiles/main.dir/build.make:206: recipe for target 'CMakeFiles/main.dir/eval.c.o' failed
make[2]: *** [CMakeFiles/main.dir/eval.c.o] Error 1
CMakeFiles/main.dir/build.make:134: recipe for target 'CMakeFiles/main.dir/arith_yylex.c.o' failed
make[2]: *** [CMakeFiles/main.dir/arith_yylex.c.o] Error 1
My CMakeLists.txt looks like this:
cmake_minimum_required (VERSION 2.6)
project (dash)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include config.h -DBSD=1 -DSHELL -DIFS_BROKEN")
add_executable(main main.c alias.c arith_yacc.c arith_yylex.c cd.c error.c eval.c exec.c expand.c histedit.c input.c jobs.c mail.c main.c memalloc.c miscbltin.c mystring.c options.c parser.c redir.c show.c trap.c output.c bltin/printf.c system.c bltin/test.c bltin/times.c var.c alias.h arith_yacc.h bltin/bltin.h cd.h error.h eval.h exec.h expand.h hetio.h init.h input.h jobs.h machdep.h mail.h main.h memalloc.h miscbltin.h myhistedit.h mystring.h options.h output.h parser.h redir.h shell.h show.h system.h trap.h var.h mktokens mkbuiltins builtins.def.in mkinit.c mknodes.c nodetypes nodes.c.pat mksyntax.c mksignames.c)
What can I do to make it build? If I use the makefile and build with make then it builds. But I want to build with CMake.

"Does not name a type"-Error when compile with CMake

when I compile my code in Eclipse it works fine. But the process fails if I try to compile it with CMake. It says:
error: ‘default_random_engine’ does not name a type
default_random_engine generator;
error: ‘uniform_int_distribution’ does not name a type
uniform_int_distribution distribution;
and some more errors, which I believe are the consequences of these two.
class randomInt
{
private:
int m_max;
default_random_engine generator;
uniform_int_distribution<int> distribution;
public:
randomInt(int max = 0) :
m_max(max),
generator(time(0)),
distribution(0, m_max)
{}
int operator ()()
{
return distribution(generator);
}
};
int main(int argc, char **argv)
{
vector<int> vec(100);
generate(vec.begin(), vec.end(), randomInt(100));
ostream_iterator<int> streamIt(cout, ",\n");
copy(vec.begin(), vec.end(), streamIt);
return 0;
}
There is my CMakeLists.txt
project(TEST)
# States that CMake required version must be greater than 2.6
cmake_minimum_required(VERSION 2.8)
# Setup sources
set(TEST_SOURCES
aufgabe2_1.cpp
aufgabe2_2.cpp
aufgabe2_3.cpp
aufgabe2_4.cpp)
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -std=c++11")
# Build executable
add_executable(main ${TEST_SOURCES})
These are symbols from the C++ standard library's pseudo-random number generation, but you haven't qualified the symbols with the namespace std, so use std::default_random_engine and std::uniform_int_distribution.