多个包含警卫在MSVC ++ .NET中工作......对吗? [英] Multiple Inclusion Guards Work in MSVC++ .NET...right?

查看:44
本文介绍了多个包含警卫在MSVC ++ .NET中工作......对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很沮丧。我是来自Unix / Linux阵营的资深C ++程序员,

试图学习Visual C ++。我正在尝试构建一个项目,其中我需要在一些不同的文件中包含一个标题,但是

经典的多重包含问题让我很难咬我。

#ifndef ..#define ..#endif方法似乎无法正常工作,虽然

我读过的所有文档都表明它应该。


作为一个小例子,我有一个包含三个文件的空控制台项目:

globals.h,functions.cpp和driver.cpp。它们看起来像这样:


// ------------------------------- ---------------------

// globals.h

#ifndef _GLOBALS_

#define _GLOBALS_


const char * msg =" Wiggety wack";


#endif

// EOF


// -------------------------------- --------------------

// functions.cpp

#include< iostream>

使用命名空间std;


#include" globals.h"


void getWiggety()

{

cout<< msg<<结束;

}

// EOF


// -------------- --------------------------------------

//司机。 cpp

#include< iostream>

使用命名空间std;


#include" globals.h"


extern void getWiggety(void);


int main(无效)

{

cout<< msg<<结束;

getWiggety();

返回0;

}

// EOF


这不会链接,因为msg被声明了两次,尽管我还没有#b $ b #ifndef ..#define ..#endif in globals.h。我已经走了,看着

< iostream>,并且它以同样的方式保护免受多次包含

我正在这样做。我也把它包括在两个地方但是...链接器

并没有抱怨std :: cout等。 al。


WTFO?


ff

解决方案

Fritz Foetzl < FR ********** @ hotmail.com>在消息中写道

news:d2 ************************** @ posting.google.c om ...

我很沮丧。我是来自Unix / Linux
阵营的资深C ++程序员,试图学习Visual C ++。我正在尝试构建一个项目,其中我需要在一些不同的文件中包含一个标题,但是经典的多重包含问题让我很难受。
#ifndef ..#define ..#endif方法似乎无法正常工作,虽然我读过的所有文档都表明应该这样做。
作为一个小例子,我有一个空的控制台项目,包含三个
文件:globals.h,functions.cpp和driver.cpp。它们看起来像这样:

// ----------------------------------- -----------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_

const char * msg =" Wiggety wack";

#endif
// EOF

// -------------- --------------------------------------
// functions.cpp #include< iostream>
使用命名空间std;

#include" globals.h"

void getWiggety()
{
cout<< msg<<结束;
}
// EOF

// -------------------------- --------------------------
// driver.cpp
#include< iostream>
使用namespace std;

#include" globals.h"
extern void getWiggety(void);

int main(void)
{
cout<< msg<< endl;
getWiggety();
返回0;
}
// EOF




我认为你的问题有与包括警卫无关。标题

" globals.h"被正确包含两次,每次文件时都包含一次

" driver.cpp"和functions.cpp编译。问题是msg

在同一个程序中被定义了两次,违反了ODR。


你可能应该对它进行decalre extern并在 global.cpp",

或使用内联函数代替


inline msg()

{

static const char * s =" Wiggety wack";

返回s;

}


Jonathan


Fritz Foetzl写道:

我很沮丧。我是来自Unix / Linux阵营的资深C ++程序员,试图学习Visual C ++。我正在尝试构建一个项目,其中我需要在一些不同的文件中包含一个标题,但是经典的多重包含问题让我很难受。
#ifndef ..#define ..#endif方法似乎无法正常工作,虽然我读过的所有文档都表明应该这样做。
作为一个小例子,我有一个包含三个文件的空控制台项目:
globals.h,functions.cpp和driver.cpp。它们看起来像这样:

// ----------------------------------- -----------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS _




你需要一个新的成语来包含警卫名称。以

开头的标识符后跟一个大写字母或另一个下划线的

保留用于任何目的的实现。除非你确定你知道更好,否则请避免使用以下划线开头的标识符。


但这不是你的问题。也不是多重包容。包括

警卫防止在

单个翻译单元中出现多次同样的事情,但不会出现在两个不同的b
不同的东西翻译单位。例如,如果我在

a.cpp中有一个main()函数,并且我在b.cpp中也有一个main()函数,我的程序就不会链接。

这基本上就是你在做什么 - 你有'msg''出现在两个

..cpp文件中,因为每个文件都有它自己的#included globals.h副本。


这样做的正确方法是只在标题中声明''msg''(

''extern'',并且没有初始化)然后在某处的.cpp文件中定义(沿初始化的

)。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。


" Kevin Goodsell" <我们********************* @ neverbox.com>写在

消息新闻:DR ***************** @ newsread2.news.pas.earthl ink.net


然而,这不是你的问题。也不是多重包含。
包括警卫防止在单个翻译单元中出现多次的同一事件,但不是针对出现在两个不同翻译单元中的同一事物。例如,如果我在a.cpp中有一个main()函数,并且我在
b.cpp中也有一个main()函数,我的程序就不会链接。这基本上就是你在做什么 -
你有'msg''出现在两个.cpp文件中,因为每个文件都有自己的#included copy of globals.h。

正确的方法是只在标题中声明''msg''(
''extern'',没有初始化)然后定义它(和
一起)在一个.cpp文件的某个地方初始化。




我同意这是最好的方法,但我认为这个策略是

OP应该仍然有效。这是因为const变量默认情况下应该具有

内部链接,即


const char * msg =" Wiggety wack";


应相当于


static const char * msg =" Wiggety wack";


后者将肯定是没有问题,前者也应该建立。


-

John Carson

1.回复电子邮件地址,删除donald

2.不要回复电子邮件地址(在此处发帖)


I''m flummoxed. I''m a veteran C++ programmer from the Unix/Linux camp,
trying to learn Visual C++. I''m trying to build a project in which I
need to include one header in a couple of different files, but the
classic multiple inclusion problem is biting me hard. The
#ifndef..#define..#endif method doesn''t seem to be working, although
all the documentation I''ve read indicates that it should.

As a small example, I have an empty console project with three files:
globals.h, functions.cpp and driver.cpp. They look like this:

// ----------------------------------------------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_

const char *msg = "Wiggety wack";

#endif
// EOF

// ----------------------------------------------------
// functions.cpp
#include <iostream>
using namespace std;

#include "globals.h"

void getWiggety ()
{
cout << msg << endl;
}
// EOF

// ----------------------------------------------------
// driver.cpp
#include <iostream>
using namespace std;

#include "globals.h"

extern void getWiggety (void);

int main (void)
{
cout << msg << endl;
getWiggety ();
return 0;
}
// EOF

This won''t link, because msg is declared twice, in spite my
#ifndef..#define..#endif in globals.h. I''ve gone and looked at
<iostream>, and it''s protected against multiple inclusion the same way
as I''m doing it. I''m also including it in two places but...the linker
doesn''t complain about std::cout et. al.

WTFO?

ff

解决方案

"Fritz Foetzl" <fr**********@hotmail.com> wrote in message
news:d2**************************@posting.google.c om...

I''m flummoxed. I''m a veteran C++ programmer from the Unix/Linux camp, trying to learn Visual C++. I''m trying to build a project in which I
need to include one header in a couple of different files, but the
classic multiple inclusion problem is biting me hard. The
#ifndef..#define..#endif method doesn''t seem to be working, although
all the documentation I''ve read indicates that it should.

As a small example, I have an empty console project with three files: globals.h, functions.cpp and driver.cpp. They look like this:

// ----------------------------------------------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_

const char *msg = "Wiggety wack";

#endif
// EOF

// ----------------------------------------------------
// functions.cpp
#include <iostream>
using namespace std;

#include "globals.h"

void getWiggety ()
{
cout << msg << endl;
}
// EOF

// ----------------------------------------------------
// driver.cpp
#include <iostream>
using namespace std;

#include "globals.h"

extern void getWiggety (void);

int main (void)
{
cout << msg << endl;
getWiggety ();
return 0;
}
// EOF



I think your problem has nothing to do with include guards. The header
"globals.h" is properly included twice, once when each of the files
"driver.cpp" and "functions.cpp" is compiled. The problem is that msg
is being defined twice in the same program, violating the ODR.

You should probably decalre it extern and define it in "global.cpp",
or use an inline function instead

inline msg()
{
static const char* s = "Wiggety wack";
return s;
}

Jonathan


Fritz Foetzl wrote:

I''m flummoxed. I''m a veteran C++ programmer from the Unix/Linux camp,
trying to learn Visual C++. I''m trying to build a project in which I
need to include one header in a couple of different files, but the
classic multiple inclusion problem is biting me hard. The
#ifndef..#define..#endif method doesn''t seem to be working, although
all the documentation I''ve read indicates that it should.

As a small example, I have an empty console project with three files:
globals.h, functions.cpp and driver.cpp. They look like this:

// ----------------------------------------------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_



You need a new idiom for include guard names. Identifiers beginning with
an underscore followed by an upper case letter or another underscore are
reserved for the implementation''s use for any purpose. Unless you are
sure you know better, avoid identifiers that begin with an underscore.

This is not your problem, however. Nor is it multiple inclusion. Include
guards protect against the same thing appearing multiple times in a
single translation unit, but not against the same thing appearing in two
different translation units. For example, if I have a main() function in
a.cpp and I also have a main() function in b.cpp, my program won''t link.
This is basically what you are doing - you have ''msg'' appearing in both
..cpp files, because each has it''s own #included copy of globals.h.

The proper way to do this is to only declare ''msg'' in the header (with
''extern'', and no initialization) and then have its definition (along
with the initialization) in a .cpp file somewhere.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


"Kevin Goodsell" <us*********************@neverbox.com> wrote in
message news:DR*****************@newsread2.news.pas.earthl ink.net


This is not your problem, however. Nor is it multiple inclusion.
Include guards protect against the same thing appearing multiple
times in a single translation unit, but not against the same thing
appearing in two different translation units. For example, if I have
a main() function in a.cpp and I also have a main() function in
b.cpp, my program won''t link. This is basically what you are doing -
you have ''msg'' appearing in both .cpp files, because each has it''s
own #included copy of globals.h.

The proper way to do this is to only declare ''msg'' in the header (with
''extern'', and no initialization) and then have its definition (along
with the initialization) in a .cpp file somewhere.



I agree that that is the best way to do it, but I think that the strategy of
the OP should still work. This is because const variables should have
internal linkage by default, i.e.,

const char *msg = "Wiggety wack";

should be equivalent to

static const char *msg = "Wiggety wack";

The latter will certainly build without a problem and so should the former.

--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


这篇关于多个包含警卫在MSVC ++ .NET中工作......对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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