Linking sdl2 (undefined reference to SDL functions) in chicken scheme - sdl

I am trying to learn chicken scheme by writing a simple game using sdl. I am trying to create a foreign function to initialize SDL:
(use foreigners lolevel)
(foreign-declare "#include <SDL2/SDL.h>")
(define (sdl-init)
(foreign-lambda* int ((int val))
"if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \"Couldn't initialize SDL: %s\",
SDL_GetError());
exit(1);
}
"))
I get the errors:
undefined reference to `SDL_Init'
undefined reference to `SDL_GetError'
undefined reference to `SDL_LogError'
So it can't find SDL. Normally in C you would do something like this (from the sdl page):
gcc -o test test.c sdl-config --cflags --libs
is there a way to add these flags when compiling .scm files.

I would recommend using the sdl2 egg. If you still prefer to build your own bindings to sdl2, you can use something like this:
csc test.scm -C "`sdl-config --cflags`" -L "`sdl-config --libs`"
Also I note that you're still on CHICKEN 4, which is no longer maintained; you might want to consider updating to CHICKEN 5.

Related

How to include and use any of the stuff in the subdirectories of `C:\msys64\mingw64\include`

In my VSCode environment with working msys2 / mingW64 /gcc compilers i'm now trying to use ncurses / curses.
msys is in the default installpath, so gcc / g++ /gdb are in
C:\msys64\mingw64\bin
As i understand it, the headerfiles being used by directives are the ones in
C:\msys64\mingw64\include.
So in general i think this is a question how to properly include and use any of the stuff in the subdirectories of C:\msys64\mingw64\include
For my case with ncurses, in include directory there are two subdirectories with names ncurses and ncursesw with identical content.
To begin, i try with a very simple `helloCurses.cpp file
#include <ncurses/curses.h>
using namespace std;
int main(int argc, char ** argv)
{
// init screen and sets up screen
initscr();
// print to screen
printw("Hello World");
// refreshes the screen
refresh();
// pause the screen output
getch();
// deallocates memory and ends ncurses
endwin();
return 0;
}
obviously the compiler can find the ncurses/curses.h file.
But it still doesn't compile.
I'm getting the error messages:
Executing task: C/C++: MingW g++.exe build active file
Starting build...
C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.cpp -o D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Mathias\AppData\Local\Temp\cc44BFtA.o: in function `main':
D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:7: undefined reference to `__imp_initscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:10: undefined reference to `__imp_printw'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:13: undefined reference to `__imp_refresh'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_stdscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_wgetch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:19: undefined reference to `__imp_endwin'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
As far as I understand the linker will not find the ncurses library. This should be libncurses.a or libncurses.dll.
These files are in the directory C:\msys64\mingw64\lib.
In the end i like to compile with VSCode tasks.json, but to begin with, the commandline
gcc -I C:\msys64\mingw64\include\ncurses -L C:\msys64\mingw64\lib -lncurses helloCurses.cpp -o helloCurses.exe
didn't work either and still gives me the same errormessage.
I have been pointed to
Why does the order in which libraries are linked sometimes cause errors in GCC?
I'm sure this will at some point become valuable information, but right now this isn't about the order of a multitude of probably codependent libraris. This is just about how to include a very first additional library not being basic enough to be includd by a simple
#include <stdio.h>, but still basic enough to be in the full toolchain being installed bypacman -S --needed base-devel mingw-w64-x86_64-toolchain

Undefined references to GLFW / Vulkan although libraries seem to be linked

I'm a C++ beginner with some experience in Java trying to set up a project using GLM, GLFW, and Vulkan on Windows. This is going to be my first time getting my hands dirty in a lower-level language like C++. I'm having a lot of trouble getting the compiler to link the Vulkan and GLFW libraries to my project. I'm following the tutorial here at vulkan-tutorial.org to get started. Here's the code in main.cpp:
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream>
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Here are the commands being used to compile it:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -IC:/glfw-3.2.1/include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1
The first command compiles the .cpp into a .o successfully, but the second command gives me errors from the linker. Every single reference I made to a member from Vulkan or GLFW is undefined. (Path has been shortened for easier reading)
[omitted]/src/main.cpp:12: undefined reference to `glfwInit'
[omitted]/src/main.cpp:14: undefined reference to `glfwWindowHint'
[omitted]/src/main.cpp:15: undefined reference to `glfwCreateWindow'
[omitted]/src/main.cpp:18: undefined reference to `vkEnumerateInstanceExtensionProperties#12'
[omitted]/src/main.cpp:26: undefined reference to `glfwWindowShouldClose'
[omitted]/src/main.cpp:27: undefined reference to `glfwPollEvents'
[omitted]/src/main.cpp:30: undefined reference to `glfwDestroyWindow'
[omitted]/src/main.cpp:32: undefined reference to `glfwTerminate'
It seems like the linker can't find the library files I provided with -L and -l, but if I change -lglfw3 to -llibglfw3.a or -lglwf3.dll, I get this:
[omitted]/mingw32/bin/ld.exe: cannot find -llibglfw3.a
or
[omitted]/mingw32/bin/ld.exe: cannot find -lglfw3.dll
Leading me to think that the linker DID find the libraries at first, since it didn't complain about being unable to find the library - but why can't it find sources for the references to GLFW / Vulkan functions? I have no idea what's happening. Is it finding the library files?
I'm using GLFW 3.2.1, Vulkan SDK 1.1.82.1, MingW GCC version 6.3.0, and I'm running on Windows 10 Pro 64-bit.
Turns out my problem was rooted in using an old version of MingW. Originally I downloaded MingW from here, which was recommended by the guide on MingW's wiki. I updated to 8.1.0 from this site and the linker began throwing errors about undefined references to different functions, such as "__imp_CreateDCW" and "__imp_SwapBuffers".
I happened to recognize these as GDI functions from some other research I did. I added GDI32 to the libraries option in the linker and the build went through successfully.
Now my build commands are as follows:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1 -lgdi32

OpenCV: Undefined reference to xcb_poll_for_reply

As of late I have been getting the following error whenever I try to compile any program that uses the open cv libraries, I use g++ to compile:
g++ Example.cpp -o Ex `pkg-config opencv --cflags --libs`
No matter the content of the file (I have checked with programs that worked a couple of weeks ago) I always get the following error:
/usr/lib64/libX11.so.6: undefined reference to `xcb_poll_for_reply64'
/usr/lib64/libX11.so.6: undefined reference to `xcb_wait_for_reply64'
Do you have any idea of what might be the cause? (and how to fix it)
An example program that fails to compile:
#include "path/opencv2/highgui/highgui.hpp"
#include "path/opencv/highgui.h"
using namespace cv;
int main (int argc, char * argv[])
{
Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE) ;
return 0;
}
Add -lxcb to your command line (this will instruct the linker linking w/ the xcb library). Please make sure the 64b version of xcb is in the linker path (you can always put it explicitly via the -L switch)
The error was caused by some changes done to the libX11.so.6, talked with the FE machines support and they fixed it.

Unable to link to SDL2 functions using MinGW

I'm relatively new to programming and I've decided to give SDL a try, but I'm a bit stuck. I haven't been able to build the project in codeblocks and I get 'undefined reference' to all SDL functions. I've seen a lot of similar questions here, but none of the solutions seems to help. I've already added the \include\SDL2 and the \lib folders to search directories, I've added SDL2Main and SDL2 to link libraries in linker options, I've even added -mwindows to other linker options. Also, I tried linking against the 64-bit version as well, but things got even worse.
Here's my source code, pretty much copied straight out of the tutorial I started:
#include <SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main(int argc, char* args[])
{
// initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if(g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return 1; // sdl could not initialize
}
// everything succeeded lets draw the window
// set to black // This function expects Red, Green, Blue and
// Alpha as color values
SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
// show the window
SDL_RenderPresent(g_pRenderer);
// set a delay before quitting
SDL_Delay(5000);
// clean up SDL
SDL_Quit();
return 0;
}
And here's the build log:
mingw32-g++.exe -LC:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib -o bin\Debug\GeometryProject.exe obj\Debug\main.o -mwindows C:\MinGW\lib\libmingw32.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2main.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2.a
obj\Debug\main.o: In function `SDL_main':
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:7: undefined reference to `SDL_Init'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:13: undefined reference to `SDL_CreateWindow'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:17: undefined reference to `SDL_CreateRenderer'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:27: undefined reference to `SDL_SetRenderDrawColor'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:29: undefined reference to `SDL_RenderClear'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:31: undefined reference to `SDL_RenderPresent'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:33: undefined reference to `SDL_Delay'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:35: undefined reference to `SDL_Quit'
C:\MinGW\lib\libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
9 errors, 0 warnings (0 minutes, 0 seconds)
Is there anything else I could try? I really would like to get this running and would appreciate any help.
A possible problem is that your linking to a SDL2 library for a different architecture.
You should be using
SDL2-2.0.1\i686-w64-mingw32
instead of
SDL2-2.0.1\x86_64-w64-mingw32
Also after setting the library search path use this notation to link to libraries
-lmingw32 -lSDL2main -lSDL2
It's much easier to read.
Instead of adding full path, try -LC:/PATH_TO_SDL -lSDL2main -lSDL2 (or only one SDL option) instead; the lib and .a is already known by the linker.
(not sure if you have to use either / or \)
Since you are using C++, replace your include with this:
extern "C"
{
#include "SDL.h"
}
This tells the compiler to treat SDL as C - and not C++ - code.
See also: SDL2 won't link properly

SDL2 won't link properly

I'm using Code::Blocks, that's my code:
#include "SDL2/SDL.h"
int main(int argc, char* args[]) {
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Quit();
return 0;
}
I'm building like:
mingw32-g++.exe -o C:\..\main.exe C:\..\main.o -lmingw32 -lSDL2main -lSDL2
And getting that:
undefined reference to "SDL_Init"
undefined reference to "SDL_Quit"
I'm pretty sure the linker finds the libs cause if I change them to something random it complains "can't find whatever".
A bit late, but I just stumbled over a similar problem on Linux.
This results in linker errors:
g++ $(pkg-config --cflags --libs sdl2) sdl2test.cpp
sdl2test.cpp:(.text+0x11): undefined reference to `SDL_Init'
sdl2test.cpp:(.text+0x20): undefined reference to `SDL_GetError'
sdl2test.cpp:(.text+0x34): undefined reference to `SDL_Quit'
This works:
g++ sdl2test.cpp $(pkg-config --cflags --libs sdl2)
even if this is an Linux Problem, i came throug this post via google.
Mayb it could help someone else: i had the same Problem with Windows XP 32bit Codeblocks: Mingw + SDL2 and i fixed it after i copied the right SDLFolders (include, lib...) to the mingw-folder. The reason why i trapped to this pifall is that the naming of the SDL-Rootfolder, out of the DEV-Pack is a bit confusing. You got a "x86_64-w64-mingw32"-Folder which is for 64bit Compiler and "i686-w64-mingw32" which is for 32bit Compiler (like the moste still are). i messed this up because of the "x86..."naming, still dont know why they are writing it this way.
After overwriting the right files it works fine for me.
Are you sure you have your libraries in you path ?
Try adding -LC:/whatever/ with the folder that actually contains you libSDL2.a and other *.a to your compiler's arguments.