Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
Quintus opened this Issue · 5 comments

4 participants

@Quintus

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

@monaka

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.

@monaka

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
@bovi

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.

@monaka

@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.

@bovi

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.