GCC没有自动解析.cpp从.h类包括 [英] GCC not resolving automatically .cpp from .h class include

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

问题描述

我的问题类似于这一个,但是我无法确定如何在eclipse中修复它。



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



该类我正在尝试包含在一个单独的项目中,该项目已正确链接。



以下是一个示例:



项目来源



文件myclass.h

 code> #ifndef MYCLASS_H_ 
#define MYCLASS_H_

命名空间lol
{
类myclass {public // definitions ...}
}
//#include myclass.cpp // **当我取消注释这个**
#endif

文件myclass.cpp

  #includemyclass.h// **不工作,除非我包含我的.cpp(unity build like)** 
//我不想包含.cpp文件
namespace lol {// not funny

myclass: :myclass(){
} //代码...代码
}

其他项目
mainFile.cpp

  #includemyclass.h//只有在最后包含myclass.cpp的myclass.h 

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

可以通过从一个我喜欢的解决方案的makefile中构建所有内容来解决这个问题,但是我需要使用eclipse,可悲的是,

任何建议?



谢谢!

解决方案

你在文件末尾缺少一个#endif



使用#pragma once。

  //。 h文件
#pragma一次

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

//文件结尾

看到我的解释的编译单元和流程此处


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类包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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