将C编译的静态库链接到C ++程序 [英] Linking C compiled static library to C++ Program

查看:182
本文介绍了将C编译的静态库链接到C ++程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个静态库(使用gcc编译)链接到一个c ++程序,并得到'未定义的引用'。我在Ubuntu 12.04服务器机器上使用了gcc和g ++版本4.6.3。例如,下面是factorial方法的简单库文件:

mylib.h

  #ifndef __MYLIB_H_ 
#define __MYLIB_H_

int factorial(int n);

#endif

mylib.c

  #includemylib.h

int factorial(int n)
{$ ((n> = 1)?(n *阶乘(n-1)):1);
}

我使用gcc创建了这个mylib.c的对象:

  gcc -o mylib.o -c mylib.c 

静态库也是使用AR实用程序从目标文件创建的:

  ar -cvq libfact.a mylib.o 

我用C程序(test.c)和C ++程序(test.cpp)



C和C ++程序都具有相同的主体:

  #includemylib.h
int main()
{
int fact = factorial(5);
返回0;

$ / code>

假设静态库libfact.a在/ home / test目录中可用,I编译我的C程序没有任何问题:

pre $ gcc test.c -L / home / test -lfact

code>

然而,在测试C ++程序时,它抛出一个链接错误:

  g ++ test.cpp -L / home / test -lfact 

test.cpp :(。text + 0x2f):undefined对`factorial(int)'的引用
collect2:ld返回1退出状态

我甚至尝试在test.cpp中添加extern命令:

  extern int factorial(int n)//在main()函数之前添加

仍然是同样的错误。


  • 有人可以告诉我我在这里犯了什么错误吗?静态库?

  • 是否必须在我的 test.cpp 中添加任何内容才能使其工作?


解决方案

问题是,您还没有告诉您的C ++程序使用C语言编写阶乘。您需要更改你的test.h头文件。像这样

  #ifndef __MYLIB_H_ 
#define __MYLIB_H_

#ifdef __cplusplus
externC{
#endif

int factorial(int n);

#ifdef __cplusplus
}
#endif

#endif

现在您的头文件应该适用于C和C ++程序。详情请参阅以了解详情。



包含双下划线的BTW名称保留给编译器(所以名称以下划线和大写字母开头),所以 #ifndef __MYLIB_H _ 严格地说是非法的。我会更改为 #ifndef MYLIB_H #define MYLIB_H


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?

解决方案

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

这篇关于将C编译的静态库链接到C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆