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?

10
  • Header files are to declare functions. You need to provide the implementation of func_i_want_to_include in a separate cpp file. (unless the function in the header is a template function) Commented May 17, 2018 at 14:11
  • 3
    "unresolve external symbol" means it can't find a symbol. How does main.cpp know about call_to_file_b()? Commented May 17, 2018 at 14:12
  • 1
    @eozd That is incorrect. They are separated by convention there is no reason that a header cannot contain implementation. Commented May 17, 2018 at 14:12
  • i have try to seperate the implementation to a .cpp , i still get the same results Commented May 17, 2018 at 14:12
  • 3
    How did you compile the code? Commented May 17, 2018 at 14:15

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the most complete answer and includes all comments and previous partial answers. Should be marked as the correct answer.
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.

3 Comments

separation didn't help here
i try to compile at visual studio 2015 x86 release moede
You have to provide more complete information, because there has to be some other issue with the setup. Did using inline work for you?
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_

4 Comments

Are you building my_function.cpp along with the rest of cpp files? my_function.cpp includes my_function.h too?
yes i compile it all together and my_function.cpp includes my_function.h
there isn't extern keyword for function that i can use of?
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.
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.