2

In projectA.vcproj

fileA.h

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

fileA.c

void functionA()
{
//function defined
}

In projectB.vcproj:

fileB.h

void functionB() ;

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.

4
  • 2
    Did you forget to #include fileA.h from fileB.cpp? Commented Jul 13, 2012 at 17:14
  • @dasblinkenlight: No, the error message for that is "error: ‘functionA’ was not declared in this scope" Commented Jul 13, 2012 at 17:18
  • Yes, I have included it. Sorry I forgot to mention it here. Thanks. I made an edit just now. Commented Jul 13, 2012 at 17:19
  • You should show the command line that is producing the error. Commented Jul 13, 2012 at 17:21

4 Answers 4

4

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.

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
Sign up to request clarification or add additional context in comments.

Comments

1

You need to include the header file of functionA in functionB's header file. So in fileB.h add the line #include "fileA.h"

Comments

1

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. If you omit the header from fileA.c and if you compile the code using:

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().

However, you should not compile C code with g++; that is asking for trouble.


When originally asked, fileB.cpp didn't include any headers.

fileB.cpp

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

1 Comment

+1 Linking issue is certainly the problem, because the error message points to the linker.
0

How do you compile?

gcc filea.c fileb.cpp

Should do the Trick.

2 Comments

That probably won't work; gcc probably will reject the C++ source, and it doesn't address the source code level issues in the C++ code.
@JonathanLeffler: It will compile the C++ source as C++ (GCC chooses language based on extension by default) but it won't link in the standard C++ library, which will probably cause link errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.