Computing.Net > Forums > Programming > Help with creating a library in GCC

Help with creating a library in GCC

Reply to Message Icon

Original Message
Name: mike
Date: July 6, 2002 at 07:46:07 Pacific
Subject: Help with creating a library in GCC
Comment:

Hi, I have the GCC compiler for linux and I
am having a problem when I am trying to make
a library. My library here doesn't do anything
useful, its just an excercise to get it to
work Here is what I am doing:

my header file:
// Test.h
void run();

my .cpp file for my header:
// Test.cpp
void run() {
int i = 5;
}

Now I compiler Test.cpp like this

g++ -c Test.cpp

It seems to work all good and ok now I create
the library:

ar rs Test.a Test.o

It seem to work too and now I make my file
that uses my library:

// uses.cpp
#include "Test.h"
int main() {
run();
}

By the way, all this is done in /home/michael/
cpp_code/
Now I compiler it like this:

g++ -L "Test.a" uses.cpp

and I get this

/tmp/ccQGwfDs.o: In function `main':
/tmp/ccQGwfDs.o(.text+0x7): undefined reference to `run(void)'
collect2: ld returned 1 exit status

Why do I get this? I use the -L to inlcude the
library so when I goes to link it together it
will be able to find it.
Please Help, I have been trying to figure this
out for some time now.


Report Offensive Message For Removal

Response Number 1
Name: Tom
Date: July 6, 2002 at 17:24:31 Pacific
Subject: Help with creating a library in GCC
Reply: (edit)

Try:
g++ -L/home/michael/cpp_code/Test.a uses.cpp
(without a space between L and /).


Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: July 6, 2002 at 17:30:29 Pacific
Subject: Help with creating a library in GCC
Reply: (edit)

You are confusing/misusing -L and -l (that's a small L)

-L adds a directory to the list of directories that the linker will search for the required libraries.

-l tells the linker to include a specific library.

Many times when linking your own library, you'll use both.

g++ -L /usr/local/lib/mylibs -lmylib uses.cpp

The -L tells it to look in /usr/local/lib/mylibs for libraries and the -lmylib says to look for a library called libmylib.a or possibly mylib.a

In my experience, libraries are always named libXXX.a and the -l command is -lXXX

So -lXXX would find a library named libXXX.a but I don't know if it would find XXX.a

Also, I don't know if -L is necessary if the library is in the current directory.

Anywho, your link command might look something like:

g++ -L. -lTest uses.cpp

or

g++ -lTest uses.cpp

or you may have to make you library named libTest.a and use

g++ -lTest uses.cpp


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Help with creating a library in GCC

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software