C++ GNU GCC with #define out & colliding - c++

I am working with Code::Blocks and C++ GNU GCC and trying to port my Visual Studio Project to GNU GCC.
And while I am trying to compile I get stuck with my define macro from a Defines.h file:
#define out &
And when compiling I get this error:
error: expected unqualified-id before '=' token
On this line:
// Open for output. Default for #c ofstream and fstream.
static const openmode out = _S_out;
In ios_base.h
As I know, that file is a part of a libstd library. Can I disable it? If so, how can I do that?
My library doesn't requiring any std c++, just c library.

There are two main options:
Don't define a macro using the word out.
Declare the macro after you include all of your headers.
But seriously, a macro named out that becomes & is a pretty awful macro. It's going to make it difficult to understand the code very quickly.

Related

How to compile C code that #includes C++ code?

I have to compile generated C code that #includes C++ headers and it get it to work on Linux, OS X and Windows. I put the #includes myself to be able to interact with C++ code from it.
The C code however is such that a strict compiler would catch errors that could be treated as warnings. I want to pass these off as warnings as most of this is generated code which I don't have control over.
Here are my attempts to compile it (I use CMake, btw):
(I use these flags: -Wno-write-strings -Wno-error -fpermissive)
Treat the entire project as C++ code: works on Linux (with g++) but on OS X I get:
With Clang:
error: assigning to 'char *' from incompatible type 'const char *'
With g++:
/var/folders/hf/5dbkbqrx5p3bmnpdqqy4wgpr0000gn/T//ccvMHo2S.s:18:no such instruction: `vmovdqa LC0(%rip), %xmm3'
/var/folders/hf/5dbkbqrx5p3bmnpdqqy4wgpr0000gn/T//ccvMHo2S.s:19:no such instruction: `vmovdqa LC1(%rip), %xmm2'
/var/folders/hf/5dbkbqrx5p3bmnpdqqy4wgpr0000gn/T//ccvMHo2S.s:20:no such instruction: `vmovdqu (%rsi), %xmm7'
/var/folders/hf/5dbkbqrx5p3bmnpdqqy4wgpr0000gn/T//ccvMHo2S.s:21:no such instruction: `vmovdqu 16(%rsi), %xmm8'
/var/folders/hf/5dbkbqrx5p3bmnpdqqy4wgpr0000gn/T//ccvMHo2S.s:22:no such instruction: `vmovdqa LC2(%rip), %xmm1'
/var/folders/hf/5dbkbqrx5p3bmnpdqqy4wgpr0000gn/T//ccvMHo2S.s:23:no such instruction: `vpshufb %xmm3, %xmm7,%xmm6'
Treat C code as C code:
Doesn't find <atomic>
fatal error: atomic: No such file or directory
#include <atomic>
Use -Wa -q flags as per this answer:
https://stackoverflow.com/a/19339870/3684931 (I made a mistake there. It should have been -Wa, -q)
Flag becomes invalid.
clang: error: unknown argument: '-q'
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Use -Wa,-q flags as per this answer:
https://stackoverflow.com/a/19339870/3684931
Flag is still invalid.
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
clang: error: unsupported argument '-q' to option 'Wa,'
How do I go about building this?
You seem to be solving this problem backwards, to me. Every conforming C++ implementation seems to have a compatible C ABI, so C++ projects can use C libraries. Use that to your advantage.
Create C libraries containing your C code, and compile them as C code (e.g. cc -c your_filename.c will produce a your_filename.o object file).
Create a C++ project (where you should be able to include all of your C headers, if you design them carefully), and use your linker to link to the your_filename.o object files you produced in step 1, when compiling that project.
I put the #includes myself to be able to interact with C++ code from it.
This is where you went wrong. #includeing a C++ header from a .c file will never work.
Instead, you need to make header files which are compatible with both C and C++. These files should only contain code in the common subset of C and C++ ; and they should also contain:
#ifdef __cplusplus
extern "C" {
#endif
// main header code here
#ifdef __cpluscplus
}
#endif
so that the C++ units including this header create calls using the C ABI.
In this header , you can declare some functions that will do the interacting with the C++ code which your C files require. And you can implement those functions in a new C++ source file which you write.

Error: expected initializer before ': ' token , gcc compiler

I encounter this error when I am trying to compile a c++ code via a Makefile.
error: expected initializer before ':' token
I have checked the compatibility of the compiler of my system
I also checked the paths etc. I also did some test; such as adding a semicolon after the 2nd declaration of class but didnt work. I have little to no experience with c++, the script is not even written by me; it is part of vtk library (Visualisation toolkit). Part of the script from where
the error generates is:
#ifndef __vtkProcessObject_h
#define __vtkProcessObject_h
#include "vtkAlgorithm.h"
class vtkDataObject;
class VTK_FILTERING_EXPORT vtkProcessObject : public vtkAlgorithm
{
public:
vtkTypeRevisionMacro(vtkProcessObject,vtkAlgorithm);
void PrintSelf(ostream& os, vtkIndent indent);
I get the error in line 8.
Probably it is something really straightforward, but as I said I have no clue how this language works.
The VTK_FILTERING_EXPORT macro is defined in a header, and is largely there for Windows and/or GCC symbol visibility. You don't mention what version of VTK you are compiling, but using CMake to generate the Makefiles would ensure the correct include paths are set up. If this is Linux, and the GCC visibility functionality has not been activated you could define the macro to nothing, but I suspect you would hit many other issues once you got past this point in the compilation.

MinGW screw up with COLORREF and RGB

I am trying to build a 3rd party open source project using MinGW. One of the dependencies is wxWidgets. When I try to make the project from MSYS I get a compiler error from
/MinGW/msys/1.0/local/include/wx-2.8/wx/msw/private.h
In function 'COLORREF wxColourToRGB(const wxColour&)':
error: cannot convert 'RGB' to 'COLORREF {aka long unsigned int}' in return
This is somewhat odd given that, according to Microsoft the RGB macro returns a COLORREF.
In fact, looking in H:\MinGW\include I find wingdi.h with the following code
#define RGB(r,g,b) ((COLORREF)((BYTE)(r)|((BYTE)(g) << 8)|((BYTE)(b) << 16)))
What sort of thing would cause this error? Is there some way I can check to see if COLORREF and RGB are being included from wingdi.h and not somewhere else? Is that even worth checking?
Specifications
GCC version 4.7.2
wxWidgets version 2.8.12
(I'm new to C++ and MinGW specifically but generally computer and programming literate)
Try preprocessing the source with -E and check what is actually happening with the macro expansion or whether something is redefining the macro.
The preprocessed source undoubtedly will be very lengthy after #includeing its header dependencies. Your text editor's search function, grep and findstr are your friends here.

Cygwin gcc compiled fails in IDE complaining about 'exit' undeclared

When I compile a program using just
gcc code.c
There are no messages, and an output file is generated successfully. The outputted file works. However, when I try to the same cygwin installation's gcc compiler in an IDE (I've tried Netbeans and Dev-C++), I get the following errors
main.cpp:27: error: `exit' undeclared (first use this function)
main.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:77: error: `write' undeclared (first use this function)
main.cpp:78: error: `close' undeclared (first use this function)
I don't see what's different. Why does it not compile?
OK, the issue was that in the IDE, the file had a .cpp extension, whereas when I was compiling from a terminal, it had a .c extension. So, my new question is why does it not compile when it's treated as a c++ file. Isn't C a subset of C++?
C++ is stricter then C. Where C allows you to call a function without a prototype, C++ does not allow this.
To solve the problem, you want to add:
#include <stdlib.h>
Also, when compiling at the command line. Make sure to use the -Wall flag so you'll get important warnings:
gcc -Wall code.c
The IDE is using fussier options to the compiler. You need to include some headers:
#include <stdlib.h> // exit()
#include <unistd.h> // close(), write()
The default options allow almost anything that might be C to compile. By the looks of it, the IDE sets '-Wmissing-prototypes' as one of the compiler options.
If you compile code with a C++ compiler, you must ensure that all functions are declared before use. C is sloppier (or can be sloppier) about that - it is recommended practice to ensure all functions are declared before being defined or referenced, but it is not mandatory. In C++ it is not optional.
There is a subset of C that is also a subset of C++; there are bits of C that are not C++, and there are many bits of C++ that are not C. In particular, an arbitrary C program is not, in general, a C++ program. For example, a C program may not declare 'exit()' and yet it can both use it and still compile. A C++ program must declare 'exit()' before it can user it and compile.
You will have to use g++ for compiling .cpp files.
One possible reason may be that the IDE is unable to access the include files, the cygwin gcc compiler may be expecting it in /usr/include(not sure), and the dev-cpp may not be able to access it.

Defining _LIBC in C++ Program

Some of the GNU library code can be have enhanced debugging if the flag _LIBC is set. While using -D_LIBC when compiling gives the error message
"/usr/include/gnu/stubs.h:7:3: #error Applications may not define the macro _LIBC"
So how do you define this variable?
You'd define it when compiling LibC. It's Open Source, after all.