How do I link and compile Raylib on windows? - c

I have installed the raylib migw from the official source. The installation directory is C:\raylib\raylib. I have written the sample program from the website as follows.
#include "raylib.h"
int main(void){
const int screenWidth = 800;
const int screenHeight = 600;
InitWindow(screenWidth, screenHeight, "raylib basic window test");
SetTargetFPS(60);
while(!WindowShouldClose()){
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
I have compiled it using gcc main.c -I C:\raylib\raylib\src and when I do this, I'm getting the following error
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0x37): undefined reference to `InitWindow'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0x43): undefined reference to `SetTargetFPS'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0x48): undefined reference to `WindowShouldClose'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0x58): undefined reference to `BeginDrawing'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0x86): undefined reference to `ClearBackground'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0xce): undefined reference to `DrawText'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0xd3): undefined reference to `EndDrawing'
C:\Users\jaych\AppData\Local\Temp\ccOHQPti.o:main.c:(.text+0xdd): undefined reference to `CloseWindow'
collect2.exe: error: ld returned 1 exit status
How do I fix this?

Add libraylib.a file into lib folder, then run the following command
gcc main.c -o main -O1 -Wall -std=c99 -Wno-missing-braces -L ./lib/ -lraylib -lopengl32 -lgdi32 -lwinmm

Related

Compiling a sample SDL2 program with gcc/cygwin on windows: undefined reference to WinMain

I browsed the whole internet and tried every solution for this, without result.
What I'm using:
Windows 10
Cygwin
gcc compiler
I'm trying to compile this simple code:
#include "SDL.h"
int main(int ac, char **av)
{
return 0;
}
This is what my makefile looks like:
hellosdl:
gcc hellosdl.c -L../SDL2-2.0.18/x86_64-w64-mingw32/lib -lSDL2main -lSDL2 -mwindows -I../SDL2-2.0.18/x86_64-w64-mingw32/include/SDL2
where -lSDL2main -lSDL2 -mwindows is the output of sdl2-config --libs, minus -lmingw32.
Output:
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/10/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.2.0-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status
make: *** [Makefile:3: hellosdl] Error 1
What am I not understanding?

Using SDL(Simple DirectMedia Layer) Libraries in Ubuntu

i'm trying to include all SDL libraries that i have already installed and exist in '/usr/include/SDL' this directory contains all .h files
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
SDL_Surface *screen=NULL ;
SDL_Init (SDL_INIT_VIDEO);
if (SDL_Init(SDL_INIT_VIDEO !=0))
{
printf("erreur");
return 1 ;
}
screen=SDL_SetVideoMode (400,400,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen== NULL)
{
printf("Unable to set video mode : %s \n",SDL_GetError() );
return 1;
}
SDL_Flip (screen);
SDL_Quit ();
return 0;
}
when i compile code above that exists in main.c in terminal using this command:
gcc main.c -o program -LSDL
i get this :
axemaster#ubuntu:~/Desktop/stackoverflow$ gcc main.c -o program -LSDL
/tmp/ccMx1per.o: In function `main':
main.c:(.text+0x16): undefined reference to `SDL_Init'
main.c:(.text+0x20): undefined reference to `SDL_Init'
main.c:(.text+0x53): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x63): undefined reference to `SDL_GetError'
main.c:(.text+0x88): undefined reference to `SDL_Flip'
main.c:(.text+0x8d): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status
How can i solve this ?

compiling with libssl does not work

I have a very simple code:
#include <openssl/sha.h>
int main() {
SHA_CTX sha1;
SHA_Init(&sha1);
}
I have installed both libssl-dev and libcrypto++-dev:
However I have a build failure using the following command:
$ gcc -lcrypto -lssl main.c
/tmp/ccfnCAxT.o: In function `main':
main.c:(.text+0x1f): undefined reference to `SHA1_Init'
collect2: error: ld returned 1 exit status
$
$ gcc -lssl main.c
/tmp/ccfnCAxT.o: In function `main':
main.c:(.text+0x1f): undefined reference to `SHA1_Init'
collect2: error: ld returned 1 exit status
Platform: Ubuntu 16.04
-lssl is not needed, -lcrypto is enough, and it must be at the end:
gcc -o main main.c -lcrypto
(or whatever you want your program to be called goes after -o)

Cygwin and SDL 2.0 : how to compile?

I've downloaded SDL2.0 like this:
~/tmp>wget http://www.libsdl.org/tmp/release/SDL2-2.0.0.tar.gz
Then I extracted it, went into the directory, and did ./configure && make && make install. Everything went fine. Now I've made a very simple sample (see far below). My problem is when I try to compile it doesn't work, So I copied SDL2.dll into the current directory and tried all combinations I know without success:
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -l SDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -lSDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -L SDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2.dll
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -l SDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -lSDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -L SDL2
~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2
All errors are either:
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: cannot find -lSDL2.dll
collect2: error: ld returned 1 exit status
Or:
933012#F9ZW00118371 ~/tmp/SDL2-2.0.0 >gcc -o tt tt.c -I/home/933012/tmp/SDL2-2.0.0/include -LSDL2.dll
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xe): undefined reference to `SDL_Init'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x42): undefined reference to `SDL_CreateWindow'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x50): undefined reference to `SDL_GetWindowSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x67): undefined reference to `SDL_RWFromFile'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x77): undefined reference to `SDL_LoadBMP_RW'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0x9c): undefined reference to `SDL_UpperBlit'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xa7): undefined reference to `SDL_UpdateWindowSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xb2): undefined reference to `SDL_FreeSurface'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xda): undefined reference to `SDL_PollEvent'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xef): undefined reference to `SDL_DestroyWindow'
/cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o:tt.c:(.text+0xf4): undefined reference to `SDL_Quit'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/933012/AppData/Local/Temp/ccHZGVRX.o: bad reloc address 0x20 in section `.eh_frame'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
How would you do?
Here's my source code I try to compile:
#include <SDL.h>
int main(int argc, char *argv[]) {
int running;
SDL_Window *window;
SDL_Surface *windowsurface;
SDL_Surface *image;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
592, 460, SDL_WINDOW_SHOWN);
windowsurface = SDL_GetWindowSurface(window);
image = SDL_LoadBMP("exampleimage.bmp");
SDL_BlitSurface(image, NULL, windowsurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_FreeSurface(image);
running = 1;
while (running) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
running = 0;
break;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Not sure if this will work for cygwin, but on my Mac using pkg-config I was able to find out the compiler flags:
gcc example.c $(pkg-config --cflags --libs sdl2)
you should link with this flags: -Lwhere/sdldll/is -lSDL2, and -Iwhere/sdlincludes/are
-L flag is where the binaries(dll and lib) are, and -l is the filename of the dll(or lib)
-I flag is where the include files are located(.h)
gcc -o program.exe main.cpp -LSDL2/bin -lSDL2 -ISDL2/include

compilation error on Executing embedded lua in C file

I am using Cygwin environment with Lua Interpreter package included while cygwin installation.
So I am able to compile and run sample lua progs.
But when i try to execute a sample c file which has lua calls , i am always getting this following error.
$ cc -o ../samples/ctest -Wall ../samples/ctest.c
/tmp/ccOYgLj4.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status
My sample ctest.c file contents:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* lua interpreter */
lua_State* l;
int main () {
int dofile;
/* initialize lua */
l = lua_open();
/* load lua libraries */
luaL_openlibs(l);
/* run the hello.lua script */
dofile = luaL_dofile(l, "hello.lua");
if (dofile == 0) {
/* call foo */
lua_getglobal(l,"foo");
lua_call(l,0,0);
}
else {
printf("Error, unable to run hello.lua\n");
}
/* cleanup Lua */
lua_close(l);
return 0;
}
hello.lua file contents:
print("from c hurray")
on searching the net everywhere they say some linker error and have to include -llua51. So i tried the following .
$ cc -o ../samples/ctest -Wall -llua5.1 ../samples/ctest.c
/tmp/cc3v5Nim.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status
Vedhashree#Vedhashree-PC /cygdrive/c/cygwin/bin
$ ls /usr/lib/liblua*.a
/usr/lib/liblua.a /usr/lib/liblua5.1.a
/usr/lib/liblua.dll.a /usr/lib/liblua5.1.dll.a
Can you help me fix this issue and make my first embedded lua c program work?
Update:
$ cc -o ctesing -Wall ctesting.c -llua5.1
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua5.1
collect2: ld returned 1 exit status
-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua51
collect2: ld returned 1 exit status
-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua51
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua
collect2: ld returned 1 exit status
-----------------------------------------------------------------
Still I get only these errors :(
Place -llua5.1 after ../samples/ctest.c. Objects should be linked in reverse order of dependency.
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua5.1
UPDATE: Your update describes a different problem. In this case the linker cannot find a liblua5.1.a file in its search path. Make sure that you have such a library on your system and try adding its path using the -L option.

Resources