top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Does g++ really need to compile main, if c++ code is involved?

+2 votes
253 views

We have a big bunch of C and C++ code in our product and compile main with g++, because I read in

http://www.parashift.com/c++-faq/overview-mixing-langs.html

that this is needed.

Can we get rid of this condition? For example by not using exceptions, which are not caught and not using global/static non pod variables and not using class wide non pod variables?

I tried to find answers to that, but didn't find anything. Most people don't seem to be aware of that "compile main with c++-compiler" rule. If I missed some resource, please excuse.

posted Mar 20, 2014 by Deepankar Dubey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

It's not exceptions only for which this is required. C++ performs "name-mangling" on the symbols which doesn't happen in C. I'm not sure if I have enough context here to answer decisively, however, I think this FAQ link you've provided assumes that you're wishing to compile a C++ program and link with C sources. It is possible to do the opposite. Interestingly, the very FAQ you linked to says as much and that you should continue reading that section for how to perform this. In fact, this link even states, "BTW there is another way to handle this whole thing: compile all your code (even your C-style code) using a C++ compiler."

I'll bet the rest of the FAQ in section 32 will be most interesting reading for you.

answer Mar 20, 2014 by Satish Mishra
Similar Questions
0 votes

I'm not a c++/template guru and i can't decide if this is really a valid code or i have encountered a gcc bug:

template  struct A {
 void *p;
};
template  struct B : A {
 void *foo() { return p; }
};

g++ says "error: 'p' was not declared in this scope". microsoft's compiler is happy with the same code.

Can anyone help?

0 votes

This code compiles on MSVC but not on G++ :

template
class B
{
public:
 static void SomeMethod()
 {
 A::SomeMethod();
 }
};

class A
{
public:
 static void SomeMethod();
};

void test()
{
 B::SomeMethod();
}

The error with g++ is :

test.cpp: In static member function "static void B::SomeMethod()":
test.cpp:7:3: error: "A" has not been declared

Is there is a option to indicate to g++ to evaluate the template functions only when a specialization is called, and not before ?

...