I tried to link a static library (compiled with gcc) to a c++ program and I got 'undefined reference'. I used gcc and g++ version 4.6.3 on a ubuntu 12.04 server machine. For example, here is the simple library file for factorial method:
mylib.h
#ifndef __MYLIB_H_ #define __MYLIB_H_ int factorial(int n); #endif mylib.c
#include "mylib.h" int factorial(int n) { return ((n>=1)?(n*factorial(n-1)):1); } I created object for this mylib.c using gcc:
gcc -o mylib.o -c mylib.c Again the static library was created from the object file using AR utility:
ar -cvq libfact.a mylib.o I tested this library with a C program (test.c) and C++ program (test.cpp)
Both C and C++ program have the same body:
#include "mylib.h" int main() { int fact = factorial(5); return 0; } Assuming static library libfact.a is available in /home/test directory, I compiled my C program without any issues:
gcc test.c -L/home/test -lfact However while testing C++ program, it threw a link error:
g++ test.cpp -L/home/test -lfact test.cpp:(.text+0x2f): undefined reference to `factorial(int)' collect2: ld returned 1 exit status I even tried adding extern command in test.cpp:
extern int factorial(int n) //added just before the main () function Still the same error.
- Can someone tell me what I am wrong here?
- Is there anything I missed while creating the static library?
- Do I have to add anything in my
test.cppto make it work?