-1

I'm trying to include a function like this in C++ and I can't understand why it's not working. I have 3 files.

test.cc

int test() {
  std::cout << "This is a test" << std::endl;
}

test.h

int test();

main.cc

#include "test.h"

int main() {

    test();
    return 0;
}

This is the error I got and the command I used.

c++ main.cc -o main
Undefined symbols for architecture x86_64:
  "test()", referenced from:
      _main in main-12ba52.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
daniula
  • 6,898
  • 4
  • 32
  • 49
Francis
  • 919
  • 3
  • 14
  • 23
  • What is the error that you're getting? Post it please. I suspect this is a linker problem, not a compiler problem. – Steve Jul 15 '14 at 20:15
  • 2
    What error do you get? – Ranic Jul 15 '14 at 20:15
  • Nothing wrong with the include, as long as `test.cc` is listed for compilation, howover it wont compile. `std::cout` is not declared. You must `#include ` in `test.cc`. – Havenard Jul 15 '14 at 20:17
  • 2
    test.cc is missing some #includes – Paul R Jul 15 '14 at 20:18
  • @Ranic I added the error and command to my question. – Francis Jul 15 '14 at 20:19
  • 2
    `c++ main.cc -o main` only compiles `main.cc`. You also need to compile `test.cc` into `test.o`, then link `test.o` and `main.o` together into an executable. – Steve Jul 15 '14 at 20:20
  • This error is telling you that even though you declared `test()`, it was not defined. It happened because `test.cc` was not included in your project for compilation. – Havenard Jul 15 '14 at 20:21
  • Can anyone give me an example makefile for compiling two files together? I tried to make one myself but I kept getting the same linker error. – Francis Jul 15 '14 at 20:21
  • c++ main.cc test.cc -o main – S. Miller Jul 15 '14 at 20:23
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Jul 15 '14 at 20:38

4 Answers4

6

Here's the culprit.

c++ main.cc -o main

You need to link test.o.

c++ main.cc test.cc -o main

user3813353
  • 174
  • 4
3

Assuming you are using your compiler properly, try to include "test.h" on the top of your test.cc file:

#include "test.h"

int test() {
   std::cout << "This is a test" << std::endl;
}

Compile with:

g++ main.cc test.cc -o main
haccks
  • 104,019
  • 25
  • 176
  • 264
Marco
  • 131
  • 1
  • 8
2

You should add the following to test.cc:

#include "test.h"
#include <iostream>

And make sure you're building/linking with test.cc

Ranic
  • 486
  • 4
  • 12
1

The definition of function test has undefined behaviour because it does not return a value though it has return type int

int test() {
  std::cout << "This is a test" << std::endl;
}

As you did not report what is the error I think that the problem is that you did not include in the project file test.cc. You have to link it along with file main.cc that the linker would be able to find the definition of test.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for your answer, I hadn't actually realised that my function was wrong. The problem is that I'm getting a linker error. – Francis Jul 15 '14 at 20:19
  • @Francis You have to link the both modules together that the linker would be able to find the definition of test. – Vlad from Moscow Jul 15 '14 at 20:21