GCC不自动解析.cpp从.h类include [英] GCC not resolving automatically .cpp from .h class include

查看:177
本文介绍了GCC不自动解析.cpp从.h类include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这一个,但我不能计算如何解决它在eclipse中。



在eclipse上编译一个小程序时,我有一个wierd行为。当我将.cpp文件包含在标题的末尾(并删除.c在.cpp中的包含)时,它会编译,否则不会。





这里是一个例子:



在专案来源



档案myclass.h

 code> #ifndef MYCLASS_H_ 
#define MYCLASS_H_

namespace lol
{
class myclass {public // definitions ...}
}
//#include myclass.cpp // **当我取消注释这个**时工作
#endif

文件myclass.cpp

  #includemyclass.h我包括我的.cpp(团结构建像)** 
//和我不想包括.cpp文件
命名空间lol {//不好笑

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

在其他项目
mainFile.cpp

  #includemyclass.h /只有当我在myclass.h结尾处包含myclass.cpp时才有效

使用命名空间lol;
int main(){
myclass obj = myclass(); //给未定义的引用lol :: myclass :: myclass()
}

可以通过构建一个makefile来解决这个问题,这是一个我喜欢的解决方案,但我需要使用eclipse,可惜。



有任何建议吗?



谢谢!

解决方案

包含文件末尾缺少一个#endif



改用#pragma once。

  //。 h文件
#pragma once

命名空间lol
{
class foo {};
}

//文件结尾

的编译单元和管道此处。 / p>

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!

解决方案

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.

这篇关于GCC不自动解析.cpp从.h类include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆