0

My issue is similar to this one but I can't figure how to fix it in eclipse.

I have a wierd behavior when compiling a small program on eclipse. When I include the .cpp file at the end of the header (and remove the include of the .h in the .cpp) it compiles and otherwise not.

The class I am trying to include is in a separate project and that project is properly linked.

Here is an example:

In project Sources

file myclass.h

#ifndef MYCLASS_H_
#define MYCLASS_H_

namespace lol
{
class myclass{ public // definitions... }
}
//#include myclass.cpp //**works when I uncomment this**
#endif

file myclass.cpp

#include "myclass.h" // ** does not work unless I include my .cpp (unity build like) **
                     // and I don't want to include .cpp files
namespace lol{ // not funny

myclass::myclass(){
} //code ... code
}

In other project mainFile.cpp

#include "myclass.h" // works only if I include myclass.cpp at the end of myclass.h

using namespace lol;
int main(){
    myclass obj = myclass(); // gives undefined reference to lol::myclass::myclass()
}

I could fix this by building everything from a makefile which is a solution I like but I need to use eclipse, sadly.

Any suggestions?

Thanks!

Community
  • 1
  • 1
Johnride
  • 8,476
  • 5
  • 29
  • 39
  • 2
    please read an introduction to gcc and what a translation unit is and what linking is and what its all for. and never include .cpp files. – PlasmaHH Oct 08 '13 at 19:06
  • There's nothing similar to the question you referenced. You should really follow @PlasmaHH's advice 1st! – πάντα ῥεῖ Oct 08 '13 at 19:10
  • About the never include .cpp, I precisely want to avoid it. I know what linking is, I can use a makefile, this is about doing it in eclipse. And you should read this http://stackoverflow.com/questions/543697/include-all-cpp-files-into-a-single-compilation-unit – Johnride Oct 08 '13 at 19:10
  • What is the compilation error? Is it a linker error maybe? – superk Oct 08 '13 at 19:19

2 Answers2

1

You are missing a "#endif" at the end of the include file.

Use "#pragma once" instead.

// .h file
#pragma once

namespace lol
{
    class foo {};
}

// end of file

See my explanation of the compilation-unit and pipeline here.

Community
  • 1
  • 1
kfsone
  • 23,617
  • 2
  • 42
  • 74
  • Sorry I forgot to write the endif in my question, it is present in my code. Thank you for answering the question though. – Johnride Oct 08 '13 at 19:15
  • You'll need to update the post to include the actual compile/link error you're getting then – kfsone Oct 08 '13 at 19:19
0

I'd say if your .cpp is seen from eclipse auto makefile generation it takes it as source (translation unit) and adds it to the sources list.

If you want to include inline definitions (once), you should use different file extensions (e.g. .tcc, .icc).

You could also try to exclude it from the project's resource configurations (Right Click the .cpp, choose 'Resource Configurations -> Exclude from build').

Another option is to change the project type to 'Makefile project' and maintaining the makefiles on your own.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190