I installed SDL2 in Ubuntu, and I've been having trouble getting the sample program that comes with it to work. Here are the errors that appear.
|In function ‘int main(int, char**)’:|
‘SDL_HWSURFACE’ was not declared in this scope|
‘SDL_DOUBLEBUF’ was not declared in this scope|
‘SDL_SetVideoMode’ was not declared in this scope|
‘SDL_Flip’ was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings ===|
Here is my header (I added SDL_image in case there were some issues, but it didn't change anything).
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
#ifdef _SDL_imageH
#include <SDL_image.h>
#endif
Here are the libraries I included in the linker settings:
libSDL2.a
libSDL2main.a
libSDL2_image.a
libz.a
I added on those libraries consecutively, but none of them seemed to help my program run. Below are the other linker options:
-lmingw32
-lSDL2main
-lSDL2
-mwindows
-lm
-ldinput8
-ldxguid
-ldxerr8
-luser32
-lgdi32
-lwinmm
-limm32
-lole32
-loleaut32
-lshell32 -lversion -luuid -static-libgcc
I also included in the compiler search directories /usr/local/include/SDL2.
I also tried -lmingw64 as I have a 64-bit compiler, although I don't know whether or not that option is available to me, but I suspect my compiler may be the problem.
Another issue is that when I run grep -r SDL_HWSURFACE /usr/ none of the files in the directory (or other directories I've searched) have that text other than the main.cpp file that is used to generate the test sample.
The location of the first two errors are in this line
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
although I don't think that is extremely relevant.
SDL_SetVideoMode, as well as many other things, belongs to SDL 1.2 and no longer exists in SDL2. Please read https://wiki.libsdl.org/MigrationGuide
Related
I'm trying to compile a simple SDL program using Mingw w64. Here is my code:
test.c
#include "SDL2/SDL.h"
#include <stdio.h>
int main( int argc, char* args[] )
{
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("SDL2 Window", 100, 100, 640, 480, 0);
if(window==NULL)
{
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
SDL_Delay(3000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
and here is the command I'm using to compile the program:
g++ -o text.exe test.c -I./include -L./lib -lmingw32 -lSDL2main -lSDL2
but when I compile the program, I get hundreds of linking errors that look like this:
./lib/libSDL2.a(SDL_wasapi_win32.o): In function `WASAPI_PlatformInit':
/Users/valve/release/SDL/SDL2-2.0.22-source/foo-x64/../src/audio/wasapi/SDL_wasapi_win32.c:255: undefined reference to `__imp_CoCreateInstance'
I downloaded the library from the second link down in the windows development libraries section on the official SDL website, and took the libraries from the following directory:
SDL2-2.0.22\x86_64-w64-mingw32\lib
The contents of ./lib are:
libSDL2main.a
libSDL.a
What is the problem and how can I fix it?
You have two options, depending on your intent:
If you want to link SDL2 dynamically (this should be your default course of action), you need to add libSDL2.dll.a to your library directory. Then libSDL2.a will be ignored and can be removed. It should just work, no other changes are needed.
If you want to statically link SDL2, you need more linker flags. The exact flags are listed in sdl2.pc in the Libs.private section.
As of SDL 2.0.22, those are: -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid.
Those should be added to the right of -lmingw32 -lSDL2main -lSDL2.
You also might want to add -static to statically link everything, including the standard library. (What's the point of statically linking SDL2, when your program still needs the DLLs of the standard library to work?) It also makes the linker prefer libSDL2.a to libSDL2.dll.a if both are available, meaning you don't need to worry what's in your library directory.
I'm getting GCC compiling on windows with std::atomic and std::thread support.
I'm using mingw from here: http://winlibs.com/
I'm building with this command:
g++.exe #CMakeFiles/StrFormatsJSONTest.dir/includes_CXX.rsp -latomic -lglu32 -lopengl32 -D_WIN32_WINNT=0x600 -DWINVER=0x600 -D_WIN32_LEAN_AND_MEAN=1 -g -std=gnu++2a -o main.cpp.obj -c main.cpp
However, when using #include <atomic> the following error occurs:
[build] c:\utils\bin\mingw32\include\c++\10.3.0\ext\atomicity.h:83:9: error: '__gthread_active_p' was not declared in this scope
[build] 83 | if (__gthread_active_p())
If I define the function manually, I get a multiple definition error (so clearly it in the project somehow).
I'm including threads using:
find_package( Threads )
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT} )
I'm including latomic using:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -latomic -lglu32 -lopengl32 -D_WIN32_WINNT=0x600 -DWINVER=0x600 -D_WIN32_LEAN_AND_MEAN=1 -D__GTHREADS=1")
^ Like this because I need it in ALL targets within the project.
The library states that the function should be present if GTHREADS macro is 1:
Clearly it is, thats where the error is:
When I add a definition (weakly linked) before #include <atomic> it works... But I get other errors such as:
error: '__gthread_once_t' does not name a type; did you mean '__gthread_active_p'?
For some reason GCC isn't including any one of these:
gthr-default.h
gthr-posix.h
gthr-single.h
Found the issue, I had a file called "Signal.h" it was being included instead of GCC's <signal.h>
More info, this is an issue on windows due to case insensitive filenames.
I'm trying to write an OpenGL program which I'm compiling through MinGW. I've managed to successfully link OpenGL, GLUT, GLFW and GLM to my main.cpp file. Given the following headers:
#include "GL/glut.h"
#include "GLFW/glfw3.h"
#include "GL/glew.h"
#include "glm/vec3.hpp"
And the following cmd line:
g++ -o leaf.exe -Wall physics.cpp -mwindows lib/glut32.lib -lopengl32 -lglu32 -lglfw3dll -lglew32 -IC:/MinGW/include/GL
I manage to get it to compile successfully. However, when placing the .a files in MinGW/lib, the .dll file in source folder and the .h file in C:\MinGW\include and adding
#include "GL/glew.h"
With the following command line
g++ -o leaf.exe -Wall physics.cpp -mwindows lib/glut32.lib -lopengl32 -lglu32 -lglfw3dll -lglew32 -IC:/MinGW/include/GL
Then I get a long list of errors including:
In file included from physics.cpp:6:0:
c:\mingw\include\gl\glew.h:85:2: error: #error gl.h included before glew.h
#error gl.h included before glew.h
In file included from physics.cpp:6:0:
c:\mingw\include\gl\glew.h:1814:94: error: 'GLchar' does not name a type
typedef void (GLAPIENTRY * PFNGLBINDATTRIBLOCATIONPROC) (GLuint program, GLuint index, const GLchar* name);
My first time trying to make something without using Visual Studio or Eclipse. Been trying lots of fixes for it I've found here but nothing concrete.
Thanks for reading this far!
You need to reorder your includes:
#include "GL/glew.h"
#include "GL/glut.h"
#include "GLFW/glfw3.h"
#include "glm/vec3.hpp"
GLFW (and GLUT?) automatically include GL.h, which is what GLEW is complaining about. Not sure why you're using GLUT and GLFW in the same build, they don't exactly go together...
I am trying to use CATCH unit test suite linked below.
https://github.com/philsquared/Catch
However, I cannot succeed to make it right.
The main.cpp and test.cpp are as follows.
//main.cpp
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
//test.cpp
#include "catch.hpp"
TEST_CASE("TESTTest", "") {
CHECK(1 != 2 );
}
When this two files are located in the same folder, I could get a desired result.
However, I moved the test.cpp to the subdirectory named test. It does not work anymore as expected but generates linking errors.
My cmake setting is described below.
project(catchTest)
cmake_minimum_required(VERSION 2.8)
file(GLOB_RECURSE INCS "./*.cpp")
add_executable(${PROJECT_NAME} main.cpp ${INCS})
include_directories(.)
file(GLOB_RECURSE INCS "./*.cpp") was added to include every cpp source files located in the subdirectories. and include_directories(.) was included to let them know the definition of catch.hpp.
I am pretty shure I've done something wrong but I don't know how to fix it.
Please advise me to solve this problem.
It was run on Windows, compiled using mingw gcc-4.9.1 and generated by cmake ninja generator.
EDIT : I added the first few lines of error messages.
FAILED: cmd.exe /c cd . && C:\MinGW\bin\g++.exe CMakeFiles/catchTest.dir/main.cpp.obj CMakeFiles/catchTest.dir/main.cpp.obj CMakeFiles/catchTest.dir/test/testTest.cpp.obj -o catchTest.exe -Wl,--out-implib,libcatchTest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd .
CMakeFiles/catchTest.dir/main.cpp.obj:main.cpp:(.text+0x0): multiple definition of `Catch::getResultCapture()'
CMakeFiles/catchTest.dir/main.cpp.obj:main.cpp:(.text+0x0): first defined here
Your CMakeLists.txt is wrong, it includes main twice. If you change the add_executable-statement like this it works for me:
add_executable(${PROJECT_NAME} main.cpp test.cpp )
Hope that helps.
Kim
You have to include the right catch.hpp. In the repository there are two different includes with the same name: One in include and one called single_include. Please ensure that you are using single_include.
When you see something like linking error from a header file library such as catch, it means you have set the include path incorrectly.
Use single_include not include.
I am using Windows 7, Code::Blocks and MinGW. I have little to no experience when it comes to compiling/building anything, especially when Code::Blocks doesn't use makefiles.
I downloaded SDL2-devel-2.0.0-mingw.tar.gz (SDL Development Libraries) from http://www.libsdl.org/tmp/download-2.0.php, and I'd like to create a standalone executable using SDL2 libraries, but so far I've always had to bundle the SDL2.dll file with the executable to make it work.
I've heard that I can not static-link dynamic libraries, so my only option seems to be doing something with the source files using the file SDL2-2.0.0.tar.gz (Source Code) from the link I mentioned above. However, I do not know what I should do with those.
What I managed to try with the source files is importing the Visual Studio project to Code::Blocks and building it, but it tells me "sdl-config No such file or directory" (I do not know what triggered that). I'm also not sure if building merely gives me an executable, with which I do not know what I can do to link it to my own executable.
A fool proof idiot's step by step guide would be the best bet to solve this case.
EDIT:
I managed to compile the SDL libraries with the guide Jonas provided, and got a libSDL2.a file.
At first I only added the path of libSDL2.a to "Link libraries:" -section of Code::Blocks, but I got a bunch of errors such as "SDL_Init() not declared in this scope".
In addition to the libSDL2.a path, I also added the path of SDL2-2.0.0\include to the Compiler's search directory as well as the path of SDL2-2.0.0\build.libs to the Linker's search directory. I also wrote this to my test file: #include "SDL.h". My test file now looks like this:
#include "SDL.h"
int main( int argc, char* args[] ) {
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
}
It appears it did fix the declaration problem, but now Code::Blocks opened a SDL_mmjoystick.c file and gave me even more errors: "undefined reference to 'waveInClose#4'", "undefined reference to 'waveOutClose#4'", "undefined reference to 'joyGetNumDevs#0'" and tons of other ones.
Here's a screenshot of what's happening, note the different color of #include texts, I'm not sure why that happens: http://gyazo.com/00656a9c1e57a2bd0db1414fa7d68ced.png
I am not sure how to correctly take this library into use. Any help in this case, or should I make another question for it?
EDIT:
I added -lSDL2 to the linker options and deleted the other parameters. Now it builds fine:
mingw32-g++.exe -Wall -fexceptions -g -IC:\Users\User\Desktop\SDL2-2.0.0\include -c "C:\Users\User\Desktop\CppProjects\SDL project\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions -g -IC:\Users\User\Desktop\SDL2-2.0.0\include -c "C:\Users\User\Desktop\CppProjects\SDL project\thetestfile.cpp" -o obj\Debug\thetestfile.o
mingw32-g++.exe -LC:\Users\User\Desktop\SDL2-2.0.0\build\.libs -o "bin\Debug\SDL project.exe" obj\Debug\main.o obj\Debug\thetestfile.o -lSDL2 ..\..\SDL2-2.0.0\build\.libs\libSDL2.a C:\Users\User\Desktop\SDL2-2.0.0\build\.libs\libSDL2.a -mwindows
Output size is 945.80 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings (0 minutes, 1 seconds)
But when I try to run it, it says my computer lacks SDL2.dll, while the whole point was to static-link.
So currently I have the path to build/.libs in my Link libraries -settings, -lSDL2 in the Other linker options, and for search directories I have the path to SDL2-2.0.0/include for the compiler and SDL2-2.0.0/build/.libs for the linker.
In the build/.libs directory I can also see libSDL2.a, libSDL2.dll.a, libSDL2.la and libSDL2.lai files, which I don't know what they are.
It's not necessary to recompile the library,
SDL2 is given with static-link library named "libSDL2.a"
on the folder "SDL2-2.0.0\i686-w64-mingw32\lib\".
Just be sure to add these options to the linker :
"-lmingw32 -lSDL2main -lSDL2 -mwindows -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -static-libgcc"
on Code:Blocks at "Project / Build Options... / Linket settings / Other linker options"
These options allow you to link with what SDL2.dll was using.
You can retreive them on the file "SDL2-2.0.0\i686-w64-mingw32\bin\sdl2-config"
The magical trick is to delete or rename the file "libSDL2.dll.a"
on the folder "SDL2-2.0.0\i686-w64-mingw32\lib\".
I added a "-" before to keep it in case I need it.
I don't know why this librairy overcomes the other and a clue would be appreciated.
I tried with Code::Blocks 12.11 MinGW32 and it worked.
If you run with some projects that use dynamic-link
and some other which use static-link, you will have to
keep your librairies in two different folders knowing that
"libSDL2main.a" will be in those two.
Sorry for my writing, I'm not used to write in english.
Mike