LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-10-2007, 04:15 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
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.

/******************MAKEFILE********************************/
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)

clean:

@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
slzckboy
Member
 
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 ...

Code:
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

Quote:
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.

Code:
# makefile for libpost

CFLAGS = -g -rdynamic
ARFLAGS = rcs
OBJECTS=post.o login.o
INCLUDE= -I./


libpost.a:$(OBJECTS)
        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)



clean:
        rm $(OBJECTS)

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


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


libget.a:$(OBJECTS)
        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)

clean:
        rm $(OBJECTS)

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


FLAGS= -g -rdynamic
LDLIBS= -lget -lpost
INCLUDE=/usr/include

OBJECTS=POST.o


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

POST.o: POST.c

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

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

Code:
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
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
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
ta0kira
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?
Code:
gcc -c yourfile.cpp -o yourfile.o
ld -r yourfile.o -o yourfile_static.o -static -l3ds
ar rcs yourfile.a yourfile_static.o
ta0kira

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.
 
  


Reply


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
Error creating library file from object files using xlc montylee AIX 3 03-04-2024 11:21 AM
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
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:46 PM.

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