Linking C compiled static library to C++ Program

Prabu

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.cpp to make it work?
john

The problem is that you haven't told your C++ program that factorial is written in C. You need to change your test.h header file. Like this

#ifndef __MYLIB_H_
#define __MYLIB_H_

#ifdef __cplusplus
extern "C" {
#endif

int factorial(int n);

#ifdef __cplusplus
}
#endif

#endif

Now your header file should work for both C and C++ programs. See here for details.

BTW names containing a double underscore are reserved for the compliler (so are names starting with an underscore and a capital letter) so #ifndef __MYLIB_H_ is illegal strictly speaking. I would change to #ifndef MYLIB_H #define MYLIB_H

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

linking, compiling and running a c program with a static library

From Dev

Intel Compilers: linking a fortran compiled library to main program of C

From Dev

Intel Compilers: linking a fortran compiled library to main program of C

From Dev

Linking C program to C++ Shared Library

From Dev

C: Creating static library and linking using a Makefile

From Dev

Issue in Linking a C Library compiled with CMake in Android.

From Dev

Linux: C/C++ standard library static vs dynamic linking

From Dev

Linking C++ static Library in C using gcc

From Dev

Linking C++ static Library in C using gcc

From Dev

Qt c++ Does my program use static or dynamic linking?

From Dev

Link static Qt library to c program

From Dev

Undefined reference to ... when linking a static library in C++

From Dev

Linking a static library to a new project in Visual C++

From Dev

Static linking in C#?

From Dev

if 0 is compiled in c program

From Dev

Linking SDL in a C program

From Dev

linking with c binding of library

From Dev

Link a program to a static library, linking itself to another library

From Dev

How do you link a C++ static library to a C program?

From Dev

C++ program can not be compiled

From Dev

Linking a C++ program with SpiderMonkey?

From Dev

Standard c++ library linking

From Dev

C Standard Library linking in windows

From Dev

"undefined reference to" errors when linking static C library with C++ code

From Dev

Creating a static CUDA library to be linked with a C++ program

From Dev

Linking static library with JNI

From Dev

Linking dependency to static library?

From Dev

Cascading static library linking?

From Dev

Linking a Static Library and Parse

Related Related

HotTag

Archive