versuchen, ‘#include ‘ in VS 2010

Lesezeit: 4 Minuten

Benutzer-Avatar
John

Ich versuche, die Bibliotheksdatei stdbool.h in einem C-Programm zu verwenden. Wenn ich jedoch versuche zu kompilieren, erscheint eine Fehlermeldung, die besagt, dass Intellisense die Quelldatei stdbool.h nicht öffnen kann.

Kann mir bitte jemand sagen, wie ich Visual Studio dazu bringen würde, dies zu erkennen? Ist diese Header-Datei überhaupt gültig? Ich lese ein Buch über das Erlernen der C-Programmierung.

  • Scheinbar kennt das kalte Wetter keine Grenzen (dh es kann in der Unterwelt geschneit haben), der C99-Support beginnt endlich mit VS 2013, einschließlich stdbool.h. [blogs.msdn.com/b/vcblog/archive/2013/07/19/…

    – John

    Feb 7, 2014 at 16:51

  • @AlexandreC.: hello from 2015! The latest VS has stdbool.h now! chuckle

    – user3079266

    Sep 9, 2015 at 1:22

  • I went ahead and used VS 2015 community edition as suggested by @Mints97. It solved my problem. When you open the VS 2010 project in VS 2015 it automatically asks for upgrade of compilers being targeted by the C/C++ project. Compilers in VS 2015 are targeting C99 hence it is able to locate the missing header files.

    – RBT

    Oct 26, 2016 at 2:50


user avatar
Cody Gray

typedef int bool;
#define false 0
#define true 1

works just fine. The Windows headers do the same thing. There’s absolutely no reason to fret about the “wasted” memory expended by storing a two-bit value in an int.

As Alexandre mentioned in a comment, Microsoft’s C compiler (bundled with Visual Studio) doesn’t support C99 and likely isn’t going to. It’s unfortunate, because stdbool.h and many other far more useful features are supported in C99, but not in Visual Studio. It’s stuck in the past, supporting only the older standard known as C89. I’m surprised you haven’t run into a problem trying to define variables somewhere other than the beginning of a block. That bites me every time I write C code in VS.

One possible workaround is to configure Visual Studio to compile the code as C++. Then almost everything you read in the C99 book will work without the compiler choking. In C++, the type bool is built in (although it is a 1-byte type in C++ mode, rather than a 4-byte type like in C mode). To make this change, you can edit your project’s compilation settings within the IDE, or you can simply rename the file to have a cpp extension (rather than c). VS will automatically set the compilation mode accordingly.

Modern versions of Visual Studio (2013 and later) offer improved support for C99, but it is still not complete. Honestly, the better solution if you’re trying to learn C (and therefore C99 nowadays) is to just pick up a different compiler. MinGW is a good option if you’re running on Windows. Lots of people like the Code::Blocks IDE

  • Thanks so much for this. I’ve learned so much from just a few paragraphs! Thanks again! I’m going to try out the compilers you mentioned. Have a great Christmas!

    – John

    Dec 18, 2011 at 20:25

  • This is a good and complete answer. regarding compiler, I used DevC++ and also Code::Blocks, none of them have proper intellisense and code coloring which makes it hard to read the c codes again. Although I don’t like many of features that visual studio installs on your system by force and many bugs that still carried from version 2003 until now, it is still best in IDEs. Visual studio 2010 adds an ability to gray out the parts you put in #ifdef compiler directive which are not active. it is a lot helpful when you are tracking a compile bug

    – AaA

    Oct 23, 2012 at 7:21

  • Here are the related bug reports on Connect: 773523 and 748766. Vote up so they might think about implementing C99 and C11 missing headers.

    – vulcan raven

    Dec 6, 2012 at 11:02


  • Note that in Visual C++’s C++ mode, sizeof(bool) == 1. If mixing C and C++ code, defining bool as int on the C side will cause funny issues with structures and bool arrays.

    – jpa

    May 18, 2015 at 17:11

  • Note for the future: C99 and C++11 mode are only supported in Visual Studio 2013 and later. Attempting to compile anything that uses these on VS2012 or earlier will not work.

    – Lincoln Bergeson

    Feb 1, 2017 at 0:52

Create your own file to replace stdbool.h that looks like this:

#pragma once

#define false   0
#define true    1

#define bool int

In Visual Studio 2010 I had an issue using typedef int bool; as suggested elsewhere. IntelliSense complained about an “invalid combination of type specifiers.” It seems that the name “bool” is still special, even though it’s not defined.

  • I referenced this solution here: github.com/metabench/jsgui-node-png/issues/2 PDF archive: dropbox.com/s/dgyzrx89w6g4oit/…

    – Andre

    Feb 23, 2014 at 10:52

  • It is special for IntelliSense, because the front-end IntelliSense compiler is a C++ compiler. Specifically, in VS 2010, it is the EDG compiler. You can just ignore the IntelliSense warnings.

    – Cody Gray

    Jul 27, 2016 at 16:19

user avatar
dacap

Just as a warning, on x64 platforms, VS2017 (I’m not sure about previous versions) defines bool as a value of 1 byte on C++ (e.g. a char). So this

typedef int bool;

could be really dangerous if you use it as an int (4 bytes) in C files and as a native bool in C++ (1 byte) (e.g. a struct in a .h might have different sizes depending if you compile it with C or C++).

1351490cookie-checkversuchen, ‘#include ‘ in VS 2010

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy