Using C ++ with Objective-C, how can I fix the "Conflict declaration" typedef int BOOL "?

I have a lot of C ++ code originally built on a PC. I am trying to get it to work with Objective-C on a Mac. To this end, I created an Objective-C framework to host C ++ code and added a thin shell. But I ran into a typedef problem in my C ++ code.

When I was working with C ++ on a PC, I used the BOOL variable defined in WinDef.h. Therefore, when I moved everything around Mac, I added typedef int BOOL; to typedef int BOOL; to make sure the BOOL variable will still compile as expected.

But when I try to compile, I get the error message: " Conflicting declaration 'typedef int BOOL' ". I assume this is because BOOL is a keyword in Objective-C, and so is already defined. I also can't just use Objective-C BOOL , as it is an unsigned char, not an int.

While I was looking for possible solutions, I found one that mentions undefining BOOL, but I could not find how to do it (and not do it. I know if this really works). Another suggests renaming BOOLs in C ++ files to something other than a keyword. This suggestion is not an option for me, because other projects rely on C ++ code. Ideally, any changes I make should remain in a single file, or at least should not adversely affect code on a Windows machine.

How can I define a BOOL definition for Objective-C for my C ++ files and use the C ++ definition I added? Is there a better way to deal with this problem?

If this helps, I use: Xcode 3.2.5, 64-bit and Mac OS X 10.6.6

Thanks for any guidance!

+7
source share
4 answers

Part of the discussion confuses me a bit, but instead of typedef int BOOL; how about just:

 #ifndef BOOL #define BOOL int #endif 

If you use typedef , then #undef will not work, as these are two different things. #define / #undef work with preprocessor characters that do replacements, while typedef is part of the language that creates an alias of another type.

The preprocessor symbol can be undefined at any point, because it is just an instruction for the preprocessor that tells it that it no longer uses this definition when making replacements. However, a typedef cannot be undefined, because it is what is created in a specific area, not the linear processing that occurs using the preprocessor. (Likewise, you do not expect to be able to declare a global variable int x; and then at some point in your code you can say "stop recognizing x as a variable.")

The reason I suggest #define in my answer is because it is possible that ObjectiveC #define gets caught only when compiling some code. This might be an explanation of why you might get errors in your C ++ when you deleted typedef , but still get a conflict if it is found. But, if the assumptions are true, once you check the definition earlier to try to determine it, you should be able to avoid conflicting definitions when they occur.

As a final note: in this particular situation, you could just put your typedef inside the check instead of #define . However, I was inclined to do it the way I did it, because it is a very common idiom, and because this block will also prevent you from defining it twice in C ++ code if it turns on twice. Probably not very convincing reasons if you really prefer typedef and know that this is not a problem in the code. :)

+5
source

AFAIK, BOOL also #define in Objective-C. You will have problems with a conflicting BOOL define, even if you succeed

 #undef BOOL 

because your type and its type do not necessarily match the size and the "signature". Should your BOOL be int , and not what Obj-C defines it as? In other words, can you not omit your #define and just use Obj-C alone?

0
source

If you could go back in time, I would say: "Don't use typedef int BOOL in your own code."

Now that you have actually done this, you are a little pickle. In general, you should avoid using external data types for your own code, except for an interface with external code. The standard types are fine, assuming you can guarantee compilation with a standard compiler on each target platform.

The most promising solution is to stop using BOOL as a type in the platform agnostic code. At the same time, you can use all kinds of preprocessor hackers to use BOOL compilation, but you may encounter some strange link errors if you don't use BOOL equally (via #includes) everywhere.

0
source

I use cmake to download the freeimage library and I use kubuntu 14.x I had a problem with

 "error: conflicting declaration 'typedef CARD8 BOOL'" 

and I thought it would be nice to share my solution with people who have this problem!

install FreeImage on Linux:

 sudo apt-get install libfreeimage-dev 

In my CMakeLists.txt file, I have:

 set(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY /usr/libs) find_path(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY, FreeImage.h) find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY, freeimage) include_directories(${FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY}) target_link_libraries(freeimage) 

And in my main.cpp I have:

 #include <FreeImage.h> #ifndef CARD8 #define BYTE CARD8 #define BOOL CARD8 #endif 

And some additional code to capture the OpenGl framework on disk:

 void generateImage(){ int w, h; // get the width and height of the OpenGL window! glPixelStorei(GL_UNPACK_ALIGNMENT, 1); GLubyte * pixels = new GLubyte[3*w*h]; glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE, pixels); FIBITMAP * image = FreeImage_ConvertFromRawBits(pixels,w,h,3 * w, 24, 0x0000FF, 0xFF0000, 0x00FF00, false); FreeImage_Save(FIF_BMP,image, "../img/text.bmp",0); //Free resource FreeImage_Unload(image); delete[] pixels; } 

I hope this helps those who have problems with this!

Relations Cahin

0
source

All Articles