Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Can’t include mruby headers in C++, _Bool undefined #1019

Closed
Quintus opened this Issue Mar 16, 2013 · 5 comments

Comments

Projects
None yet
4 participants

Quintus commented Mar 16, 2013

Hi there,

including mruby.h in a C++ program foo.cpp:

#include <iostream>
#include <mruby.h>

int main(int argc, char* argv[])
{
  std::cout << "Hi there\n" << std::endl;
  return 0;
}

results in this:

% LANG=en_US.utf8 g++ -Wall -Imruby/include foo.cpp -o foo
In file included from mruby/include/mruby.h:36:0,
                 from foo.cpp:2:
mruby/include/mrbconf.h:121:9: error: ‘_Bool’ does not name a type
In file included from foo.cpp:2:0:
mruby/include/mruby.h:124:3: error: ‘mrb_bool’ does not name a type
mruby/include/mruby.h:125:3: error: ‘mrb_bool’ does not name a type
mruby/include/mruby.h:126:3: error: ‘mrb_bool’ does not name a type

Therefore, in C++ you might want to define mrb_bool to the actual C++ bool type or include the stdbool.h header.

Compiler version:

% LANG=en_US.utf8 g++ --version
g++ (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This was tested on today’s master at 2e937a5.

Valete,
Marvin

Contributor

monaka commented Mar 17, 2013

Hmm... Officially, mruby don't support C++. But we can't cut it dead...right.

Reffering to stdbool.h in gcc, this can be fixed by adding #define _Bool bool for C++. Is this reasonable?

I can't decide it because I don' familiar to C++. Especially around type casts (as they are differ from C99).
Any comments are appreciated.

Contributor

monaka commented Mar 17, 2013

A patch candidate is like this.

diff --git a/include/mrbconf.h b/include/mrbconf.h
index b485642..030e00c 100644
--- a/include/mrbconf.h
+++ b/include/mrbconf.h
@@ -115,10 +115,18 @@ typedef short mrb_sym;
 # define strtoll _strtoi64
 # define PRId32 "I32d"
 # define PRId64 "I64d"
+# ifdef __cplusplus
+typedef bool mrb_bool;
+# else
 typedef unsigned int mrb_bool;
+# endif
 #else
 # include <inttypes.h>
+# ifdef __cplusplus
+typedef bool mrb_bool;
+# else
 typedef _Bool mrb_bool;
+# endif
 #endif

 #ifdef ENABLE_STDIO
Member

bovi commented Mar 17, 2013

Hi Marvin,

as @monaka described you have to add something to your code due to the reason that standard mruby is not optimized for C++. I did it the other day by adding:

#include "stdbool.h"

To my file and it worked fine.

Contributor

monaka commented Mar 17, 2013

@bovi, unfortunately not all C++ have stdbool.h...

/* Supporting <stdbool.h> in C++ is a GCC extension.  */

the comment in GCC's stdbool.h.

It seems there are guaranteed "exists the pre-define macro __cplusplus" and "exists embedded type bool" by recent C++ specs.
So I guess my patch is a passable one.

Member

bovi commented Mar 17, 2013

@monaka thanks for the hint! I didn't know that. I will adapt your way and have a look.

@matz matz closed this in 13cd036 Mar 17, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment