Original Post
🔒 Locked
Compiling a dynamic linked library
Advertisement
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.


quote:
Original post by BradDaBug
but are they still the equivalent of a dynamically linked library?
Basically, yes.

If you have 'Programming Linux Games', check out page 30.
If not, here's how you do it:
Creates object files foo.o and bar.o
Creates shared library
This just installs it, ready for use.
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]
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]
Yup; What he said. Useful stuff:
sample Makefile for .so library:
sample Makefile to make something which will use a dynamic library:
Snippet of loader code:
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]
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.
Advertisement