0

my main.cpp:

#include <iostream>

int foo(int arg);

using namespace std;

int main()
{
    int x = foo(22);
    cout << x;
    return 0;
}

compile command line (Ubuntu 13.10):

g++-4.8 -L. -lfoo main.cpp -o main_app

libfoo.a contains int foo(int) but I always get the same compiler error:

/tmp/cciAyTSP.o: In function `main':
main.cpp:(.text+0x19): undefined reference to `foo(int)'
collect2: error: ld returned 1 exit status
iamnp
  • 510
  • 8
  • 23
  • `foo` is not **defined** inside your code, or in better terms it's not defined or `extern` inside your translation unit. – user2485710 Jan 06 '14 at 21:24
  • 1
    How do you build libfoo.a? Could you give us a [minimal complete example](http://sscce.org)? – Beta Jan 06 '14 at 21:27
  • @user2485710 Functions do not need to be declared `extern` – Captain Obvlious Jan 06 '14 at 21:27
  • @CaptainObvlious I didn't said that ... for example this code lacks of any header or inclusions that could have helped to solve the problem. `extern` is implicit, I know that . – user2485710 Jan 06 '14 at 21:30
  • @user2485710: Header inclusion is not necessary. – Beta Jan 06 '14 at 21:30
  • @Beta if you want to avoid the definition of a function inside a translation unit, yes it is ... – user2485710 Jan 06 '14 at 21:31
  • @user2485710: If you mean avoid having the definition of `foo` in `main.cpp`, you're wrong. What do you think the `#include` directive does? – Beta Jan 06 '14 at 21:43
  • @Beta like all other libraries, it `extern` the symbol – user2485710 Jan 06 '14 at 21:44
  • 1
    @user2485710: I think it is now safe to say that neither of us knows what you're talking about. – Beta Jan 06 '14 at 21:49
  • @Beta nevermind, the op can either define the function used inside this piece of code, right in the source file, or remove the definition and include an header to an external library that will be solved when linking. Problem is that the combination of the source code and the command line doesn't make any sense. – user2485710 Jan 06 '14 at 21:52
  • 1
    I don't have any .h files but I don't need them as long as I know function signatures. – iamnp Jan 07 '14 at 07:05

2 Answers2

3

Of course it's impossible to be sure without a reproducible case, but a common error is that if the function foo is written in C then you need to put

extern "C" { int foo(int); }

in the .h file for the C++ program to let it know that the function was not written in C++.

To write a cross-language header file that will be good for both C and C++ the common approach is

#ifdef __cplusplus
extern "C" {
#endif

... C declarations ...

#ifdef __cplusplus
}
#endif
6502
  • 112,025
  • 15
  • 165
  • 265
0

In addition to 6502's answer and Xephon's suggestion, also be aware that the order of the options matter. Instead of:

g++-4.8 -L. -lfoo main.cpp -o main_app

You should write:

g++-4.8 main.cpp -o main_app -L. -lfoo

That's because ld is a single pass linker. It won't revisit library libfoo to use a symbol from it for main_app.o.

jww
  • 97,681
  • 90
  • 411
  • 885