1

I have a.c b.cpp files.

/****** a.c ******/
fun1(...)
{
     ..........

     fun2(...); /* function present in b.cpp */

     ..........
}

/******* b.cpp *******/
extern "C" int fun2(...);
int fun2(...)
{
    ..........

}

I have compiled the code as follows:

a.o:a.c b.o
    gcc -c -o a.o a.c b.o

b.o:b.cpp
    g++ -c -o b.o b.cpp

But I'm getting error as undefined reference to "fun2()". Is this the correct way of compilation or do I need to change anything.?

Alvin
  • 940
  • 2
  • 13
  • 27
  • check that your forward declaration of fun2 and definition of fun2 has the same arguments. if there are equal gcc should compile it without any errors. – loentar Jul 15 '13 at 05:31
  • As rems4e answered, you need a function prototype. But your linking is wrong too. You should compile the sources first and then link the object files at the end: `gcc -c a.o a.c && g++ -c b.o b.cpp && g++ -o myprogram a.o b.o` – Nikos C. Jul 15 '13 at 05:33
  • rems4e is not right at 100%. All "unknown" C functions has implicit declarations. See http://stackoverflow.com/questions/4914589/c-prototype-functions . You can add prototype of `fun2` into `a.c` by including header file or declaring it directly, but this is optional. When you declare `extern "C"` in cpp the symbol `fun2` appears in `b.o` unmangled and linker will see that symbol. – loentar Jul 15 '13 at 05:45

3 Answers3

5

You need to add a prototype of the function in a.c.

rems4e
  • 3,112
  • 1
  • 17
  • 24
3
extern "C" int fun2(...);

You have that line in b.cpp which is where the function is defined, which tells it to make the function have "C" linkage, but you don't have a corresponding prototype in a.c to tell it that it that the function exists.

/****** a.c ******/
extern int fun2(...);
fun1(...)
{
     ..........

     fun2(...); /* function present in b.cpp */

     ..........
}

will fix it. Or put it in a header

// b.h
#pragma once

// tell c++ you want these externs to have C linkage
#ifdef __cplusplus
extern "C" {
#endif

// list your "C" accessible functions here.
extern int fun2(...);

// end the extern scope
#ifdef __cplusplus
};
#endif

// a.c
#include "b.h"

// b.cpp
#include "b.h"
kfsone
  • 23,617
  • 2
  • 42
  • 74
  • 1
    Not exactly. It's needed in b.cpp so that the function is `extern "C"`. In a.c, only the prototype is needed, not the `extern "C"`. – Nikos C. Jul 15 '13 at 05:24
  • @loentar C compiler absolutely knows "extern", it just doesn't know extern "C" - already fixed that. – kfsone Jul 15 '13 at 05:27
  • I need to quit answering SO questions on my phone :) – kfsone Jul 15 '13 at 05:28
  • 1
    Still not correct. Only a prototype is needed. There should be no `extern` keyword at all. rems4e already provided the correct answer. – Nikos C. Jul 15 '13 at 05:29
1

Why do you construct object file from an object file? You don't need to link b.o into a.o, just build each object from its corresponding c and link everything at the end (with your main() function).

eran
  • 6,731
  • 6
  • 35
  • 52