Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-10-2007, 04:15 AM   #1
Registered: Mar 2006
Posts: 622

Rep: Reputation: 42
Compiling library files into another library file.

Hi all, I'm hoping someone with a better understanding of make files than me can help me out
I've written some code to load 3ds models, this uses the lib3ds library file. My code works well, so I've went to make it into a .a library file, however when I do this I get an error
linker input file unused because linking not done, for all the library files (lib3ds, opengl etc.) I try to link. Strangely if I don't try to link these $(LDFLAGS) it it produces a library, but when called from project file code reports the lib3ds functions missing,but not the opengl or x11.
I would be grateful if anyone could help me with my library makefile as I'm lost. Thanks.

LDFLAGS= -l3ds -L/usr/X11R6/lib -lGL -lGLU -lXxf86vm `sdl-config --cflags --libs` -lSDL_image
CC= gcc -Wall -pedantic -g -Os -ffloat-store -c
AR= ar
SOURCE_FILE= LibaryFilesVer001
OBJECTS= $(SOURCE_FILE)/SetUpScreen.o $(SOURCE_FILE)/ModelLoader.o $(SOURCE_FILE)/TextureLoader.o $(SOURCE_FILE)/simclist.o

%.o: %.c
$(CC) -o $@ $< $(LDFLAGS)

libgame.a: $(OBJECTS)
$(AR) -rs libgame.a $(OBJECTS)


@echo Cleaning up...
@echo Done.


I should add that if I call the .a file from an example (one which does not use LDFLAG) it gives me the following errors
LibaryFilesVer001/ModelLoader.c:100: undefined reference to `lib3ds_file_load'
LibaryFilesVer001/ModelLoader.c:174: undefined reference to `lib3ds_file_material_by_name'
LibaryFilesVer001/ModelLoader.c:244: undefined reference to `lib3ds_file_free'
collect2: ld returned 1 exit status
make: *** [all] Error 1

But if I show my symbols from the libary file these symbols exist

so nm -s libgame.a shows
0000000a T initmodel
U lib3ds_file_free
U lib3ds_file_load
U lib3ds_file_material_by_name

Last edited by knobby67; 04-10-2007 at 05:19 AM.
Old 04-10-2007, 06:15 AM   #2
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
here is the scenario I had yesterday.
I was making a lib called libpost.
post.c uses function calls in another library of mine called libget.

So I was mistakenly tried doing ...

gcc -c post.c $(CFLAGS) $(LDLIBS)
but the compiler complained with "linker input file unused because linking not done"

I then found this on the net

The message that gcc reported is correct. When you supply a library
(even a compiled object file like you did) to a command that does no
linking (gcc -c), gcc simply informs you that the library was not used
because no linking was done. You didn't ask it to. Since you specified
-c, "source" files are going to be compiled into .o object files, NOT into
an executable, nor is anything going to be done with any object files. Gcc
saw the extraneous object file supplied and simply ignored it.
I resolved the problem by just compiling the two librarys seperately.Not directly linking in any object code from the other library at that stage.
However libpost is still dependent on libget so any application that uses libpost must also link in libget.

I thus linked in the two libs into my application
and all worked well.

# makefile for libpost

CFLAGS = -g -rdynamic
OBJECTS=post.o login.o

        ar $(ARFLAGS) libpost.a $(OBJECTS)

login.o: login.c post.h
        gcc -c login.c $(CFLAGS)

post.o: post.c post.h

        gcc -c post.c $(CFLAGS)

        rm $(OBJECTS)

        cp libpost.a /usr/lib
        cp post.h /usr/include
# make file for libget

CFLAGS = -g -rdynamic
OBJECTS=get.o misc.o new.o stringsep.o get_free.o
LIBS = -L./

        ar $(ARFLAGS) libget.a $(OBJECTS)

get.o: get.c get.h

        gcc -c get.c $(CFLAGS) $(INCLUDE) $(LIBS)

misc.o: misc.c get.h

        gcc -c misc.c $(CFLAGS) $(INCLUDE) $(LIBS)

new.o: new.c get.h

        gcc -c new.c $(CFLAGS) $(INCLUDE) $(LIBS)

stringsep.o: stringsep.c get.h

        gcc -c stringsep.c $(CFLAGS) $(INCLUDE) $(LIBS)

get_free.o: get_free.c get.h

        gcc -c get_free.c  $(INCLUDE) $(CFLAGS)

        rm $(OBJECTS)

        cp libget.a /usr/lib
        cp get.h /usr/include
#makefile for post() binary.Replacing
#gcc -g -o post POST.c -lget -lpost

FLAGS= -g -rdynamic
LDLIBS= -lget -lpost


        gcc -o post $(OBJECTS) $(LDLIBS) $(FLAGS)


        gcc -c POST.c -I$(INCLUDE)

        rm $(OBJECTS)
Pls also note the order of the libs

LDLIBS= -lget -lpost
It didn't work the other way around i.e -lpost -lget.
I guess because -lpost is dependent on -lget.
I hope you can relate this to your problem?!?
Old 04-10-2007, 12:01 PM   #3
Registered: Mar 2006
Posts: 622

Original Poster
Rep: Reputation: 42
I think I understand what your doing, making two libs and linking to both. However my problem is lib3ds is a premade lib that I'm trying to link into my libary, them call them both from code. What I can't understand is I call other libaries opengl xorg stuff with no problems from my libiary file however when I try to use a function that used a lib3ds function it reports it as undefined reference, but has no problem with opengl xorg stuff. Can anyone advise on this.
An example of the error type is
LibaryFilesVer001/ModelLoader.c:244: undefined reference to `lib3ds_file_free'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Old 04-10-2007, 02:07 PM   #4
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Have you tried linking your generated .o file incrementally?
gcc -c yourfile.cpp -o yourfile.o
ld -r yourfile.o -o yourfile_static.o -static -l3ds
ar rcs yourfile.a yourfile_static.o

PS Maybe remove -static if you want dynamic linking to 3ds even when statically linking to your lib.

Last edited by ta0kira; 04-10-2007 at 02:12 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
problem with the location of some library files on Slackware 10.2 while compiling atk boris-78 Linux - Newbie 4 10-10-2006 01:13 PM
Error creating library file from object files using xlc montylee AIX 2 04-07-2005 07:44 AM
Command to Finding Source files for a library file jonty_11 Programming 1 03-23-2005 05:17 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration