Page 1 of 1

How to compile a library?

Posted: Tue Nov 20, 2012 10:17 am
by blackflower
Hi, this is my first post, and my question is how can i compile a library adn then use it in other projects, i tried, but it didnt worked :cry: i did the following thing:

Graphics.h

Code: Select all

void RenderSurface(SDL_Surface *src,SDL_Rect *srcrect);
Graphics.cpp

Code: Select all

#include "Graphics.h"
#include <SDL.h>

void RenderSurface(SDL_Surface *src,SDL_Rect *srcrect)
{
   SDL_BlitSurface(src,NULL,SDL_GetVideoSurface(),srcrect);
}
i use codeblocks, so i created a static library project, compiled it, and it creates a Graphics.a and Graphics.o file, then i linked the Graphics.a to the compiler, but when i try to use the lib, including Graphics.h in an other project it sais the directory doesnt exist?? :( :(

any suggestion?? im sure im compiling the lib wrong, so if anyone could explain me how to do it... :worship:

Re: How to compile a library?

Posted: Tue Nov 20, 2012 5:26 pm
by dandymcgee
You should really go grab yourself a free copy of Visual Studio 2010: Expression Edition (or 2012 if you prefer). We'll be able to help you much easier than Code::Blocks and you'll have a far superior development tool.. it's a win-win.

Re: How to compile a library?

Posted: Tue Nov 20, 2012 7:35 pm
by lalacomun
Well, i recommend you to write your own Makefile for making the library for example:

Code: Select all

EXEC=Graphics.dll
CC=g++
SRC_EXT=cpp
CFLAGS=-D _WIN_ -I c:\SDL\Include\SDL -ldl -Wwrite-strings
LFLAGS=-shared -lmingw32 -lSDLmain -lSDL 
SRC=   ---- Here the path of the source -----
Bin=   ---- Where to create the DLL -----

all: $(BIN)$(EXEC) clean

$(BIN)$(EXEC): *.o
	$(CC) -o $(BIN)$(EXEC) *.o $(LFLAGS)

*.o:
	$(CC) -c $(SRC)*.$(SRC_EXT) $(CFLAGS)

clean:
	del -f *.o

then just open CMD and change the directory to where the makefile is located, and type 'make -f makefile' if make doesnt work try 'mingw32-make -f makefile' remember you must have minGW installed on your computer ;)

Re: How to compile a library?

Posted: Tue Nov 20, 2012 8:44 pm
by dandymcgee
lalacomun wrote:Well, i recommend you to write your own Makefile for making the library for example
Well.. yeah.. that's one way to do it. Definitely not the easiest for a beginner.

Re: How to compile a library?

Posted: Wed Nov 21, 2012 7:40 am
by MarauderIIC
If it says directory doesn't exist, sounds more like a project settings problem with the one you're trying to attach to the library.

Re: How to compile a library?

Posted: Wed Nov 21, 2012 2:09 pm
by blackflower
Well, i tried with the Makefile, and it worked! :) but i must admit i dont understand half of it :lol:
but now the problem is SDL_gfx, i tried using the flag -lSDL_gfx, but it doesnt work! maybe the flag is different?? :| any suggestions??

Re: How to compile a library?

Posted: Thu Nov 22, 2012 5:26 am
by MarauderIIC
In the makefile LFLAGS line (iirc), add a -L path_to_sdl_gfx
http://www.rapidtables.com/code/linux/gcc/gcc-l.htm