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!

4
  • 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. Commented Oct 8, 2013 at 19:06
  • There's nothing similar to the question you referenced. You should really follow @PlasmaHH's advice 1st! Commented Oct 8, 2013 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 stackoverflow.com/questions/543697/… Commented Oct 8, 2013 at 19:10
  • What is the compilation error? Is it a linker error maybe? Commented Oct 8, 2013 at 19:19

2 Answers 2

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.

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

2 Comments

Sorry I forgot to write the endif in my question, it is present in my code. Thank you for answering the question though.
You'll need to update the post to include the actual compile/link error you're getting then
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.

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.