j.r.
j.r.

Reputation: 21

How to call function from multiple cpp files

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?

Upvotes: 1

Views: 1772

Answers (4)

NLag
NLag

Reputation: 75

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.

Upvotes: 2

LuisGP
LuisGP

Reputation: 431

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_

Upvotes: 0

Onur Ko&#231;
Onur Ko&#231;

Reputation: 1

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

Upvotes: -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.

Upvotes: 1

Related Questions