1

How can i include and use function in multiple cpp files?

// my_function.h
void func_i_want_to_include();

// my_function.cpp
void func_i_want_to_include()
{
    puts("hello world");
}


//main.cpp
#include "my_function.h"
#include "file_b.h"

int main()
{
     call_to_file_b();
     func_i_want_to_include();
     return 0;
}

//file_b.h
void call_to_file_b();

//file_b.cpp
#include "my_function.h"

void call_to_file_b()
{
     puts("from file b\n");
     func_i_want_to_include();
}

When i do it like this i get yield by the linker "unresolve external symbol" , i guess the linker past func_i_want_to_include() 2 times instead of understand that this is the same function.

How do i tell him ' this is the same function just call it from 2 files but don't try to make 2 copies of the same function?

j.r.
  • 21
  • 5

4 Answers4

2

Firstly, as @LuisGP mentioned, you need #ifndef if your header file is include many times. Secondly, in the associated cpp file, you need to include the header file. The header file declares the function, the cpp file describes the function. Lastly, all the cpp files have to be included when compiling (just use command line if the editor doesn't work). It goes like this:

gcc -c my_function.cpp file_b.cpp //this will make a my_function.o and a file_b.o file
gcc -o main main.cpp my_function.o file_b.o 

or for short:

gcc -o main main.cpp my_function.cpp file_b.cpp

Here is how should the files be written:

// my_function.h
#ifndef _my_function_h
#define _my_function_h

void func_i_want_to_include(); 

#endif


// my_function.cpp
#include <stdio.h>
#include "my_function.h"

void func_i_want_to_include()
{
    puts("hello world");
}


//file_b.h
#ifndef _file_b_h
#define _file_b_h

void call_to_file_b();

#endif


//file_b.cpp
#include <stdio.h>
#include "my_function.h"
#include "file_b.h"

void call_to_file_b()
{
     puts("from file b\n");
     func_i_want_to_include();
}


//main.cpp
#include "my_function.h"
#include "file_b.h"

int main()
{
     call_to_file_b();
     func_i_want_to_include();
     return 0;
}

This is my first time answering a question, if I make any mistake, tell me and I will fix it. Hope it helps.

NLag
  • 75
  • 6
  • 1
    This is the most complete answer and includes all comments and previous partial answers. Should be marked as the correct answer. – LuisGP May 19 '18 at 07:35
1

If you want to put the function definition in the header file you need to make the function inline.

// my_function.h
inline void func_i_want_to_include()
{
    puts("hello world");
}

Otherwise the compiler will create one function for each cpp file that the header file is included in, and the linker will not know which one to choose.

This is why you usually separate function declarations

void func_i_want_to_include();

from function definitions

void func_i_want_to_include()
{
    puts("hello world");
}

Where the former goes into the header file, while the later goes into the source file.

0

As eozd said, header files are for declarations. But your problem is that your header file is being included several times, one per #include clause. You can solve that adding

#pragma once

At the top of your header, or the old fashion way:

#ifndef _MY_HEADER_FILE_H_
#define _MY_HEADER_FILE_H_

// Stuff

#endif // _MY_HEADER_FILE_H_
LuisGP
  • 431
  • 3
  • 13
  • Are you building my_function.cpp along with the rest of cpp files? my_function.cpp includes my_function.h too? – LuisGP May 17 '18 at 14:17
  • yes i compile it all together and my_function.cpp includes my_function.h – j.r. May 17 '18 at 14:19
  • there isn't extern keyword for function that i can use of? – j.r. May 17 '18 at 14:20
  • extern is for global variables. Can you update the example with the full include files? What compiler are you using. I'm pretty sure the linker is not being able to relate your my_function.cpp with the rest of the program. – LuisGP May 17 '18 at 14:23
-1

You have to create a header file. Inside that file create an object and implement a public function inside of the class.

Onur Koç
  • 1
  • 1