leo
leo

Reputation: 1

undefined references

I used a static library provided by a client, and the library is not named as "lib*.a", then how to use it when I compile.

I tried to add it as library(similar to "-l*"), but then I got -l* not found, I guess it's because it tried to find a "lib*.a".

Then I put the file name with it's absolute path in the command line as a parameter, then i got "linker input file unused because linking not done", and lots of undefined references. I also tried command line to compile the cpp file, and I can get the object file without any warning or errors.

My question is how to build the project with this kind of library in Netbeans?

Another question is in the terminal, what to do after I get the object file? This one may be a little stupid, but I am new to c++, so hopefully anyone could help me with this since I am really confused and couldn't find any straight answer in google.

Thank in advance.

Upvotes: 0

Views: 340

Answers (3)

Roger Pate
Roger Pate

Reputation:

Then I put the file name with it's absolute path in the command line as a parameter, then i got "linker input file unused because linking not done".

Sounds like you specified the library when compiling, but you need to specify it when linking instead:

$ ls
main.cpp Makefile test.cpp
$ show test.cpp main.cpp Makefile
### test.cpp
  1 #include <stdio.h>
  2 void f() {
  3   puts("Hello, world!");
  4 }
### main.cpp
  1 void f();
  2 int main() {
  3   f();
  4   return 0;
  5 }
### Makefile
  1 main: main.cpp foo.a
  2 foo.a(test.o): test.o
  3 foo.a: foo.a(test.o)
$ make
g++    -c -o test.o test.cpp
ar rv foo.a test.o
ar: creating foo.a
a - test.o
g++     main.cpp foo.a   -o main
$ ./main
Hello, world!

If your makefile uses LDLIBS when compiling, which it probably does by default, then you can add foo.a to LDLIBS. I didn't do that above for the sake of a simpler example.

Upvotes: 1

Sam Miller
Sam Miller

Reputation: 24174

rename the library to libwhatever.a then link using -L /path/to/whatever -lwhatever.

Upvotes: 2

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145289

Apparently you're using a *nix compiler.

Try to specify the library path to the invocation that performs the linking.

Apparently you have specified it to an invocation with "-c" option, which doesn't link (compile only).

EDIT: example (including erors):

Alf@SpringFlower ~/leo
$ mkdir lib; cd lib

Alf@SpringFlower ~/leo/lib
$ cat >foo.cpp
int foo() { return 42; }

Alf@SpringFlower ~/leo/lib
$ g++ -c foo.cpp

Alf@SpringFlower ~/leo/lib
$ ar -s foolib.a foo.o
ar: 'foolib.a': No such file

Alf@SpringFlower ~/leo/lib
$ ar -rs foolib.a foo.o
ar: creating foolib.a

Alf@SpringFlower ~/leo/lib
$ ls
foo.cpp  foo.o  foolib.a

Alf@SpringFlower ~/leo/lib
$ cd ..

Alf@SpringFlower ~/leo
$ cat >main.cpp
extern int foo();
#include <iostream>
int main()
{ std::cout << foo() << std::endl; }

Alf@SpringFlower ~/leo
$ g++ -c main.cpp

Alf@SpringFlower ~/leo
$ g++ main.o lib/foolib.a

Alf@SpringFlower ~/leo
$ a
bash: a: command not found

Alf@SpringFlower ~/leo
$ ./a
42

Alf@SpringFlower ~/leo
$ _

Cheers & hth.,

Upvotes: 0

Related Questions