How to compile a library?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
blackflower
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Tue Nov 20, 2012 10:05 am

How to compile a library?

Post 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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How to compile a library?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lalacomun
VS Setup Wizard
VS Setup Wizard
Posts: 114
Joined: Wed Dec 28, 2011 10:18 pm
Favorite Gaming Platforms: psx, nintendo ds, gameboy advance, xbox 360, ps2
Programming Language of Choice: C++
Location: Argentina
Contact:

Re: How to compile a library?

Post 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 ;)
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How to compile a library?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How to compile a library?

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
blackflower
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Tue Nov 20, 2012 10:05 am

Re: How to compile a library?

Post 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??
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: How to compile a library?

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply