1

I made a very simple C++ program where the goal is to use functions in different files. The function justs prints out a certain message passed in a parameter. After some research, I read that for such project, I need 3 files: the main, the header and the function (apparently I shouldn't put the function code in the header file.) I now have three files:

  • main.cpp (main file)
  • simple.h (header file (function definition))
  • simple.cpp (function file)

Whenever I try to make them work together, I always get the same error:

    C:\Users\juuhu\AppData\Local\Temp\ccQRa0R0.o:main.cpp:(.text+0x43): undefined reference to `message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2.exe: error: ld returned 1 exit status

Here's the code of the three files:

main.cpp

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

using namespace std;

int main(){
    message("Hello world!");
    return 0;
}

simple.h

#ifndef SIMPLE_H_INCLUDED
#define SIMPLE_H_INCLUDED

#include <string>

void message(std::string message);

#endif

simple.cpp

#include "simple.h"

void message(std::string message){
    cout << message;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
PetitJuju
  • 41
  • 3
  • 1
    Hi! If you have questions about why your question receives downvotes, you can ask it in [meta](https://meta.stackoverflow.com/) – justANewb stands with Ukraine Jan 22 '22 at 03:17
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Jan 22 '22 at 03:20
  • Are you sure all of these are in the project? It sounds like only `main.cpp` is being built. Maybe you have the option on for using the last successful build of `simple.cpp` since the file as it is shouldn't compile. – chris Jan 22 '22 at 03:21
  • 1
    How do you compile and link them ("try to make them work together")? – Beta Jan 22 '22 at 03:24
  • Hey I know it's late, but recently I saw on a website that I had to compile each other separately: g++ main.cpp simple.cpp It worked but could you explain me why? I thought I only had to compile main.cpp – PetitJuju Feb 13 '22 at 14:00

2 Answers2

0

I was use visual studio 2019 to compile your code. First compile error output:"Error C2065 'cout': undeclared identifier message".

When I add #include<iostream> in simple.h and change simple.cpp message function the cout to std::cout, the program output "Hello world!"

  • simple.h
#ifndef SIMPLE_H_INCLUDED
#define SIMPLE_H_INCLUDED
#include<iostream>
#include <string>

void message(std::string message);

#endif
  • simple.cpp
#include "simple.h"

void message(std::string message) {
    std::cout << message;
}

you can try again, hope you are successful to compile the code and get your to want.

HenryTien
  • 105
  • 1
  • 2
-1

You haven't link the function defination file to the header file.

simple.h

#ifndef SIMPLE_H_INCLUDED
#define SIMPLE_H_INCLUDED

#include <string>
#include "simple.cpp" // include the simple.cpp file

void message(std::string message);

#endif