0

I have a 3 files in one directory, a file source.cpp, a file main.cpp, and a file header.h. Their contents is as follows:

//source.cpp
#include "header.h"
void out(){
    std::cout << "It's working properly.";
}

//header.h
#ifndef HEADER_H
#define HEADER_H
void out();
#endif

//main.cpp
#include "header.h"
int main(){
    out();
    return 0;
}

When I compile main.cpp, I receive the error message

/tmp/ccSxCGDZ.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `out()'
collect2: error: ld returned 1 exit status

I am compiling with g++. I have not compiled anything else or done any extra steps, so there may be something that I am just forgetting. I have never tried to use a header before, so please forgive me if this is a simple question.

  • Can you please add your compile command, or which IDE you are using (if any)? – The Dark Sep 06 '16 at 23:54
  • 1
    You didn't compile and/or link to the `source.cpp` object file: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400. `g++ main.cpp source.cpp` would be enough. – chris Sep 06 '16 at 23:55
  • @chris Thank you. I didn't know what the error meant at all, but this fixed it. It now works perfectly. –  Sep 07 '16 at 00:11

0 Answers0