链接错误C和C ++(未定义参考) [英] Linking error c and c++ (undefined reference)

查看:250
本文介绍了链接错误C和C ++(未定义参考)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

abc.c文件

 的#includeabc.h
INT ABC()
{
    返回10;
}

abc.h文件

  INT ABC();

mymain.cpp文件

 的#includeabc.h
诠释的main()
{
    美国广播公司();
    返回0;
}

的Makefile

  CC = GCC -O2
CP = G ++mymain:mymain.o abc.o
    $(CP)-o mymain mymain.o abc.omymain.o:mymain.cpp abc.h
    $(CP)-c mymain.cpp abc.habc.o:abc.c abc.h
    $(CC)-c abc.c abc.h清洁:
    $(RM)*的.o mymain

输出

  G ++ -c mymain.cpp abc.h
GCC -O2 -c abc.c abc.h
G ++ -o mymain mymain.o abc.o
mymain.o:在函数'主':
mymain.cpp :(文字+ 0x5的):未定义的引用`ABC()
collect2:错误:LD返回1退出状态
使:*** [mymain]错误1

为什么ABC()是未定义的参考?

更新

新abc.h

 的externC{
    INT ABC();
}

错误

  G ++ -c mymain.cpp abc.h
GCC -O2 -c abc.c abc.h
1:0文件从abc.c包括:
abc.h:1:8:错误:预期的标识符或'('前字符串常量
 为externC{
        ^
abc.h:1:8:错误:预期的标识符或'('前字符串常量
 为externC{
        ^
使:*** [abc.o]错误1


解决方案

问题是你想的C函数链接到一个C ++对象,不告诉编译器,这是你在做什么。

如果你的头文件(abc.h)是这样的:

 的externC{
    INT ABC();
}

这是可行的(如果包括成C ++源...但是当纳入C源将打破)。

您可以包围的externC{及其尾随}如图<一宏在 href=\"http://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work\">Combining C ++和C - ?怎样的#ifdef __cplusplus工作,以便适用于C和C ++列入

注意 $(CP)-c mymain.cpp abc.h Makefile中没有任何意义 - 你不想指定在你的头编译器命令行。这同样适用于该模式的两个实例。

abc.c file

#include "abc.h"
int abc()
{
    return 10;
}

abc.h file

int abc();

mymain.cpp file

#include "abc.h"
int main()
{
    abc();
    return  0;
}

Makefile

CC=gcc -O2 
CP=g++

mymain: mymain.o abc.o
    $(CP) -o mymain mymain.o abc.o

mymain.o: mymain.cpp abc.h
    $(CP) -c mymain.cpp abc.h

abc.o: abc.c abc.h
    $(CC) -c abc.c abc.h

clean:  
    $(RM) *.o mymain

output

g++ -c mymain.cpp abc.h
gcc -O2  -c abc.c abc.h
g++ -o mymain mymain.o abc.o
mymain.o: In function `main':
mymain.cpp:(.text+0x5): undefined reference to `abc()'
collect2: error: ld returned 1 exit status
make: *** [mymain] Error 1

Why abc() is undefined reference ??

UPDATE

new abc.h

extern "C" {
    int abc();
}

error

g++ -c mymain.cpp abc.h
gcc -O2  -c abc.c abc.h
In file included from abc.c:1:0:
abc.h:1:8: error: expected identifier or ‘(’ before string constant
 extern "C" {
        ^
abc.h:1:8: error: expected identifier or ‘(’ before string constant
 extern "C" {
        ^
make: *** [abc.o] Error 1

解决方案

The problem is you're trying to link a C function into a C++ object, without telling the compiler this is what you're doing.

If your header file (abc.h) looked like this:

extern "C" {
    int abc();
}

it would work (when included into a C++ source... but would break when included into a C source).

You can enclose the extern "C" { and its trailing } within macros as shown in Combining C++ and C - how does #ifdef __cplusplus work? in order to be suitable for both C and C++ inclusion.

Note that $(CP) -c mymain.cpp abc.h in your makefile does not make sense -- you don't want to specify the header on your compiler command line. This goes for both instances of that pattern.

这篇关于链接错误C和C ++(未定义参考)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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