Skip to main content
gamedev.net
🔒 Locked

Compiling a dynamic linked library

Started by BradDaBug 4 replies 650+ views
Original Post
BradDaBug
BradDaBug
I''m using KDevelop for my IDE. So far I have figured out how to compile a simple SDL app in it, but how do I compile a dll in it?
I like the DARK layout!
Null and Void
Null and Void
It''s not called a DLL in non-Windows environments. Maybe you''ll have more luck looking up how to build ''shared objects'' or SO''s (because of their .so extension). I might download KDevelop later and give it a try, but until then that''s all I can say.

BradDaBug
BradDaBug
but are they still the equivalent of a dynamically linked library?
I like the DARK layout!
Null and Void
Null and Void
quote:
Original post by BradDaBug
but are they still the equivalent of a dynamically linked library?

Basically, yes.

cliffhawkens
cliffhawkens
If you have 'Programming Linux Games', check out page 30.
If not, here's how you do it:



$ gcc -fPIC -c foo.c bar.c


Creates object files foo.o and bar.o



$ gcc -shared -Wl,-soname,libFooBar.so.1 foo.o bar.o -o libFooBar.so.1.1.1


Creates shared library



$ su
Password:
# install -m 0755 libFooBar.so.1.1.1 /usr/lib
# ldconfig
# ln -s /usr/lib/libFooBar.so.1 /usr/lib/libFooBar.so


This just installs it, ready for use.



$ gcc -o test test.c -lFooBar



Compiles test program linking to the library (don't forget to include a header that declares the library functions as 'extern')

That had better help.

[edited by - cliffhawkens on May 28, 2002 8:11:08 PM]
[email=erydo@gdnmail.net" style="color: #ff0000; text-decoration:none; cursor:help;](o= erydo =o)[/email]
Shadow Mint
Shadow Mint
Yup; What he said. Useful stuff:

sample Makefile for .so library:


  
# Values
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
CXX = cc
CFLAGS = -Wall -g -ansi -fPIC
LFLAGS = -lc
TARGET = template.so
LIB_ARGS = -shared -Wl,-soname,$(TARGET)

# Rules
all: target

target: remake $(OBJS)
$(CXX) $(LIB_ARGS) $(OBJS) -o $(TARGET) $(LFLAGS)

remake:
touch $(SRCS)

.PHONY: clean
clean:
rm -f $(OBJS)
rm -f $(TARGET)


sample Makefile to make something which will use a dynamic library:


  
# Values
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
LIB_OBJS =
CXX = cc
CFLAGS = -Wall -g -ansi
LFLAGS = -ldl
TARGET = builder

# Rules
all: target

target: remake $(OBJS)
$(CXX) $(OBJS) $(LIB_OBJS) -o $(TARGET) $(LFLAGS)

remake:
touch $(SRCS)

.PHONY: clean
clean:
rm -f $(OBJS)
rm -f $(TARGET)


Snippet of loader code:


  
typedef void *(* DLInitFuncType) (void);

void *DLInterface__process_target (char *path)
{
void *rtn;
void *handle;
char *error_val;
int failed;
DLInitFuncType init_func;

rtn = NULL;
if (path != NULL)
{
/* Set initial condition */
failed = 0;

/* Handle the dll opening */
handle = dlopen (path, RTLD_NOW);

/* Attempt to pull out the init object */
dlerror ();
init_func = (DLInitFuncType) dlsym (handle, "create_object");
if ((error_val = dlerror()) != NULL)
{
/* Error finding that symbol, report it and set to NULL */
printf ("Error: %s.\n", error_val);
failed = 1;
}
else if (init_func == NULL)
{
/* Error but value is actually NULL */
printf ("Error: Init object is actually NULL.\n");
failed = 1;
}

/* If not failed, init object */
if (!failed)
{
rtn = (* init_func) ();
if (rtn != NULL)
{
/* Do local init and save handle for use in dlclose */

}
}
}

return (rtn);
}


Obviously hack and slash to pieces, but it gives you the idea. If you want to load a specific file with out it looking through the library path, prepend the execution path to the path. eg. data/modules/visuals.so becomes /home/fred/games/XXX/data/modules/visuals.so etc.

Ciao.

[edited by - Shadow Mint on May 31, 2002 1:49:11 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.