简体   繁体   English

从.cpp文件中调用的函数,该文件在.c文件中定义

[英]function called from .cpp file which is defined in in .c file

In projectA.vcproj 在projectA.vcproj中

fileA.h fileA.h

#ifdef __cplusplus
extern "C" {
#endif
void functionA();
#ifdef __cplusplus
}
#endif

fileA.c fileA.c

void functionA()
{
//function defined
}

In projectB.vcproj: 在projectB.vcproj中:

fileB.h fileB.h

void functionB() ;

fileB.cpp fileB.cpp

#include "fileA.h"
#include "fileB.h"
void functionB() {
    functionA(); // error: undefined reference to 'functionA'
}

I am getting the error when I compile my code on Linux, please help me fix this. 在Linux上编译代码时遇到错误,请帮助我解决此问题。

You have to link the files together. 您必须将文件链接在一起。

Source code ---compile--> Object files ---link--> Application

fileA.c ------------+
                    |---> fileA.o ---------+
                +---+                      |
                |                          |
fileA.h --------+                          +--> a.out
                |                          |
                +---+                      |
                    |---> fileB.o ---------+
fileB.cpp ----------+

The "undefined reference to XXX" error message is given by the linker, after successful compilation. 成功编译后,链接器会给出“对XXX的未定义引用”错误消息。

You need to make sure all files are linked together. 您需要确保所有文件都链接在一起。

$ ls
fileA.c  fileA.h  fileB.cpp  fileB.h
$ gcc -c fileA.c
$ g++ -c fileB.cpp
fileA.c  fileA.h  fileA.o  fileB.cpp  fileB.h  fileB.o
$ g++ fileA.o fileB.o
$ ls
a.out  fileA.c  fileA.h  fileA.o  fileB.cpp  fileB.h  fileB.o

The error message is probably coming from the linker, so you need to ensure you compile both source files and link them properly: 该错误消息可能来自链接器,因此您需要确保编译两个源文件并正确链接它们:

gcc -c fileA.c
g++ -c fileB.cpp
g++ -o program fileB.o fileA.o

You should, of course, ensure that fileA.c includes fileA.h . 当然,您应该确保fileA.c包括fileA.h If you omit the header from fileA.c and if you compile the code using: 如果您省略了fileA.c的标头,并且使用以下命令编译了代码:

g++ -c fileA.c                       # g++ instead of gcc
g++ -c fileB.cpp
g++ -o program fileB.o fileA.o

Then you will get the missing reference because g++ will have created a C++ linkage functionA() but will be expecting to call a C linkage functionA() . 然后,您会得到缺少的参考,因为g++已创建了C ++链接functionA()但期望调用C链接functionA()

However, you should not compile C code with g++ ; 但是,您不应使用g++编译C代码。 that is asking for trouble. 那是自找麻烦。


When originally asked, fileB.cpp didn't include any headers. 最初询问时, fileB.cpp不包含任何头。

fileB.cpp fileB.cpp

#include "fileB.h"
#include "fileA.h"  // Provide extern "C" declaration of functionA()
void functionB() {
    functionA();
}

You need to include the header file of functionA in functionB's header file. 您需要在functionB的头文件中包含functionA的头文件。 So in fileB.h add the line #include "fileA.h" 因此,在fileB.h中添加行#include“ fileA.h”

How do you compile? 您如何编译?

gcc filea.c fileb.cpp

Should do the Trick. 应该做把戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM