sflee
sflee

Reputation: 1719

c++ separate compilation, same function used by different source files

Thank you guys, I have tried your suggestions but it is still not working. I would like to simplify my question and ask it again.
I am using eclipse on ubuntu 12.04
I put those three files into the same c++ project folder

I have three files, main.cpp, my_lib.cpp, my_lib.h

I do :

//main.cpp
#include "my_lib.h"
// using functions from my_lib.cpp


//my_lib.cpp
#include "my_lib.h"
// defining functions

then I have error : undefined reference to function
if i do: I do :

//main.cpp
#include "my_lib.h"
#include "my_lib.cpp"
// using functions from my_lib.cpp


//my_lib.cpp
#include "my_lib.h"
// defining functions

Then I have multiple define problem

Upvotes: 0

Views: 316

Answers (4)

Mateusz Grzejek
Mateusz Grzejek

Reputation: 12058

As you probably know, you cannot include one .cpp file into another, as this will cause multiple definition error from linker. Now, the correct way is to put declaration in header file:

//Utility.h
void utility_func();

Define it's body is source file:

//Utility.cpp
void utility_func()
{
  //...
}

And use it in other sources. To do this, you can either:

1. Include header containing declaration:

#include "Utility.h"

2. Declare function manually:

extern void utility_func(); //extern is optional here and assumed by default

If you get error about unresolved external, check, if your function's signature from header matches it's definition. It quite possible, that function declared in header is not defined anywhere, because you made a typo or another mistake.

Upvotes: 0

user534498
user534498

Reputation: 3984

You don't include .cpp files in C++, because .cpp files contain definition, and C++ has a rule that you can't define a function twice.

By the way,

//This is declaration, normally in .h file
int sum(int a, int b);
//This is definition, normally in .cpp file
int sum(int a, int b) { return a + b; }

If you include .cpp file, you will definitely have sum function defined mulptiple times.

You can include .h file, because C++ allows a function to be declared multple times.

For your problem, you shall include "my_lib.h".

If a function defined in my_lib.cpp is not declared in my_lib.h, you shall declare one in my_lib.h.

If you have no right to modify my_lib.h, then it means that the function in my_lib.cpp is not supposed to be used outside of my_lib.cpp, and you need to reconsider your design.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

File my_lib.cpp contains the functions definitions.

In the first case you included it in main.cpp and in my_class.cpp. The both modules now contain the same definitions. So linker issues the error because it does not know what functions to use. You broke the One Definition Rule.

In the second case you did not include my_lib.cpp in my_class.cpp and the object code for the functions was generated only once.

You shall not include one cpp module into another. You should include only headers.

The correct project will look the following way

// main.cpp
#include "my_class.h"
#include "my_lib.h"

// my_class.cpp
#include "my_class.h"
#include "my_lib.h"
// use functions inside my_lib.cpp

// my_lib.cpp
#include "my_lib.h"

EDIT: It is a bad idea so cardinally to change the original post. I think the problem is that you did not specify for linker all your modules.

Upvotes: 1

user3188799
user3188799

Reputation: 11

Here is how you should have it setup. It is bad programming practice to include .cpp files. Instead, you should only include .h files in .cpp files. The multiple define error comes from you defining the my_lib.h and my_lib.cpp in the same file. All you need to do is call the implementation and the .cpp file will already be called, since in my_lib.cpp and my_class.cpp should have their respective headers on the top. For example: my_lib.cpp should have my_lib.h on top. my_class.cpp should have my_lib.h on top. In main.cpp all you need to do is call the .h files. Same with my_class.cpp. Given below is how you should have it setup.


main.cpp

include "my_class.h"

include "my_lib.h"



my_class.cpp

include "my_lib.h"


Upvotes: 1

Related Questions