I am having trouble installing and linking KISS_SDL into my C++ project.
I am running macOS v11.2.3 and have installed SDL2, SDL2_image and SDL2_ttf with brew.
Below is the code in main.cpp that will open a simple SDL2 window:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include "kiss_sdl.h"
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to initialize the SDL2 library\n";
return -1;
}
SDL_Window * window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
680, 480,
SDL_WINDOW_SHOWN);
if(!window)
{
std::cout << "Failed to create window\n";
return -1;
}
SDL_Surface * window_surface = SDL_GetWindowSurface(window);
if(!window_surface)
{
std::cout << "Failed to get the surface from the window\n";
return -1;
}
SDL_UpdateWindowSurface(window);
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}
}
Using the terminal command below I attempt to compile and link the required libraries.
$ g++ -Wall -o main main.cpp -lSDL2 -lSDL2_image -lSDL2_ttf
When I compile without the "#include "kiss_sdl.h"" The program compiles and executes as expected. When I add this include I am getting the following error:
In file included from main.cpp:6:
./kiss_sdl.h:34:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^~~~~~~
1 error generated.
Here is the includes from kiss_sdl.h, I have added a comment in to line 34 where the code is not working:
#ifndef _kiss_sdl_h
#define _kiss_sdl_h
#ifndef RESDIR
#define RESDIR ""
#endif
#if defined(_MSC_VER)
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#elif defined(__APPLE__)
#include <SDL.h> /* Line 34 */
#include <SDL_ttf.h>
#include <SDL_image.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#endif
#include <sys/types.h>
#ifdef _MSC_VER
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <dirent.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
Please look at the issues in the project https://github.com/actsl/kiss_sdl , issue #21 was started based on this. In brief, this is building using a command line only, without using makefile. But the g++ command line that was used is wrong. In Mac, the headers and libraries are provided by framework, but this is missing in the command line. Because of the framework there is also no need to specify SDL2 directory. This has worked for a number of people using Mac, and the framework is specified in the makefile. This was a short answer, it is going to be tested in Mac, but the person doing that is likely busy and has not enough time for that right now.
Related
Basically im trying to create a game with SDL2 and Opengl, but I cant get passed this error. I've tried changing SDL_main(int argc, char *argv[]) to regular main but nothing changes. I know this error is due to the SDL_main library but I don't know how to fix it. I've been searching info for some hours now but can't figure it out, the only thing related to this problem I've found is this post, which didn't fix my problem.
Common.h
#ifndef _COMMON_H
#define _COMMON_H
/*=======STANDARD TEMPLATE LIBRARY=======*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <memory>
#include <ctime>
#include <vector>
using namespace std;
/*=======GLU/GLEW=======*/
#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>
/*=======SDL2=======*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
/*=======CLASS DEFINITIONS=======*/
#endif
Main.cpp
#include "common.h"
bool isRunning = true;
int SDL_main(int argc, char *argv[])
{
//Error Checking/Initialisation
if (SDL_Init(SDL_INIT_NOPARACHUTE & SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
return -1;
}
else
{
//Specify OpenGL Version (4.2)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Log("SDL Initialised");
}
//Create Window Instance
SDL_Window* window = SDL_CreateWindow(
"Game Engine",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_OPENGL
);
//Check that the window was succesfully created
if(window == NULL)
{
//Print error, if null
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
else
SDL_Log("Window Successful Generated");
//Map OpenGL Context to Window
SDL_GLContext glContext = SDL_GL_CreateContext(window);
//Render
//Swap Render Buffers
SDL_GL_SwapWindow(window);
//Free up resources
SDL_GL_DeleteContext(glContext);
SDL_Quit();
return 0;
}
Error output
./src/lib/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main]+0x0): multiple definition of `main'
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0): first defined here
Makefile
all:
g++ -I ./src/include -L ./src/lib -o ./Builds/Win_Build/engine main.cpp -lmingw32 -lSDL2main -lSDL2 -lmingw32 -lopengl32 -lglew32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf --verbose
Lib folder
EDIT:
I fixed it by redownloading the libraries and using the .a extension ones. And also I changed the version of Glew to 64 bits. My new lib folder:
currently I am working on a program that uses a depth file. However, when I attempt to load this file, I keep getting build errors. I am not getting any error notifications, however, so I am not sure what the problem is. The process of me trying to load the file can be seen below.
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
#define WIDTH 640
#define HEIGHT 480
#define F 524
#define CX 316
#define CY 256
int main()
{
Mat depth;
depth.create(480, 640, CV_16UC1);
FILE *fp = fopen("depth.dat", "rb");
fread(depth.data, 2, WIDTH * HEIGHT, fp);
fclose(fp);
imshow("depth image", depth);
int k = waitKey(0); // Wait for a keystroke in the window
}
I'm trying to run a basic Vulkan test using GLFW3.2 :
#include <vulkan/vulkan.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main(int argc, char *argv[])
{
if (glfwVulkanSupported())
{
std::cout << "vulkan supported !" << std::endl;
}
else
{
std::cout << "vulkan NOT supported !" << std::endl;
}
return 0;
}
Which i build using the following cmake :
cmake_minimum_required(VERSION 3.5.1)
project(vktest)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.9")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Vulkan REQUIRED)
find_package(glfw3 3.2 REQUIRED)
include_directories(${Vulkan_INCLUDE_DIRS})
message("kek")
message(${Vulkan_INCLUDE_DIRS})
message(${Vulkan_LIBRARIES})
message(${Vulkan_LIBRARY})
add_executable(vktest vulkanTest.cpp)
target_link_libraries(vktest ${Vulkan_LIBRARIES})
target_link_libraries(vktest glfw)
Building this works just fine.
But when i run the program i always get in the else statement.
I installed nvidia last driver, lunarg's vulkan sdk with the following
environment variables :
export VULKAN_SDK=/home/mathias/vulkan/VulkanSDK/1.0.42.2/x86_64
set PATH $VULKAN_SDK/bin $PATH
set LD_LIBRARY_PATH $VULKAN_SDK/lib $LD_LIBRARY_PATH
set VK_LAYER_PATH $VULKAN_SDK/etc/explicit_layer.d
Besides i can run the cube example.
Any idea of why it never returns true ?
You are probably missing a glfwInit() call.
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <cassert>
int main(int argc, const char** argv) {
bool success = glfwInit(); //< Important!
assert(success);
if(glfwVVulkanSupported()) {
puts("Success!");
}
return 0;
}
Generally, the only glfw function you should call before glfwInit() is glfwSetErrorCallback. (There may be other exceptions, but none that I am aware of.)
Your code is missing #define GLFW_INCLUDE_VULKAN , does it not?
#define GLFW_INCLUDE_VULKAN //as required by GLFW
#include "vulkan\vk_cpp.hpp"
#include "GLFW\glfw3.h"
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
if (!glfwInit()) //Initialize GLFW
{
std::cout << "GLFW not initialized.\n";
abort();
}
if (!glfwVulkanSupported()) //Any Vulkan-related function requires GLFW initialized
{
std::cout << "Vulkan not supported.\n";
abort();
}
uint32_t ext_count; // an output placeholder
auto ext_list = glfwGetRequiredInstanceExtensions(&ext_count);
std::copy(&ext_list[0], &ext_list[ext_count-1], std::ostream_iterator<const char *>(cout, "\n"));
//feed extension list into InstanceCreateInfo struct in prepare for Vulkan instance creation
vk::InstanceCreateInfo ifo{ vk::InstanceCreateFlags(), nullptr, 0, nullptr, ext_count, ext_list };
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); // Comment out this if you need OpenGL also
GLFWwindow* hWnd = glfwCreateWindow(640, 480, "Foobar", NULL, NULL); // The usual window
auto inst = vk::createInstance(ifo); // Vulkan instance creation
vk::SurfaceKHR surf{};
auto psurf = VkSurfaceKHR(surf); // you can cast vk::* object to Vk* using a type cast
auto r = glfwCreateWindowSurface(VkInstance(inst), hWnd, NULL, &psurf); //VK_SUCCESS is somehow...0
// Draw stuff here
// Finish drawing
vkDestroySurfaceKHR((VkInstance)inst, VkSurfaceKHR(surf), nullptr);//vkcpp seems have no destructor for surface object
glfwDestroyWindow(hWnd); //Destroy the surface before the window
glfwTerminate(); // Free GLFW resource
return 0;
}
if there are typos in this code free to fix them. I have no way to compile it at current location
I'm trying to compile a short openGl and GLFW code but I'm failing in..
I've included all the .h like this :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
I'm on linux so I've made a :
sudo zypper install libglfw2
When I'm compiling like this :
g++ main.cpp -lGLU
I have :
main.cpp:14:22: fatal error: GL/glfw2.h:Any file or directory
#include <GL/glfw2.h>
^
compilation terminated.
I've also tried to include #include <GL/glfw3.h> but without success.
This is my complet code :
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw2.h>
int main()
{
if (glfwInit() == false)
{
fprintf(stderr, "GLFW failed to initialise.\n");
return (-1);
}
glfwWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(640, 480, "OPENGL", NULL, NULL);
if (!window)
{
fprintf(stderr, "Window failed to create\n");
glfwTerminate();
return (-1);
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return (-1);
}
return (0);
}
Could someone know what I have to do to compile my code ?
Thanks
There is no GL/glfw2.h and never has been, and the code above uses the GLFW 3 API. See Renamed library and header file for more information about header paths.
There is no reason to use GLFW 2 for new projects, so I would avoid the libglfw2 package. Instead see if there is a GLFW 3 package.
I install SDL via brew on my mac but I can't include it!
Here is my too easy code:
#include <SDL.h>
int main(){
return 0;
}
when I compile it with cc, CC could not find SDL.h
I found that brew install SDL in Cellar but cc did not check this folder
Could you help me?
I know this post has 9 months old but if someone, somewhere in the internet try to find out how to use SDL with mac, just follow this.
DL .dmg file on the SDL website (V2).
Put SDL2.framework in /Library/Frameworks
In your code :
#include <SDL.h>
and compile with those flags :
`sdl-config --cflags --libs`
Ex :
gcc test.c `sdl-config --cflags --libs`
Use this simple code to see it working :
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
int main( int argc, char *argv[ ] )
{
SDL_Surface *screen;
if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
printf( "Can't init SDL: %s\n", SDL_GetError( ) );
return EXIT_FAILURE;
}
atexit( SDL_Quit );
screen = SDL_SetVideoMode( 640, 480, 16, SDL_HWSURFACE );
if( screen == NULL )
{
printf( "Can't set video mode: %s\n", SDL_GetError( ) );
return EXIT_FAILURE;
}
SDL_Delay( 3000 );
return EXIT_SUCCESS;
}