LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compile C code in G++ (https://www.linuxquestions.org/questions/programming-9/compile-c-code-in-g-398815/)

lucky6969b 01-02-2006 07:57 PM

compile C code in G++
 
Hi,
I have some code that is embraced by extern "C"
it compiles okay under gcc, but gives several errors when compiled under g++. Any ideas?
Thanks
Jack

matthalliday 01-02-2006 08:24 PM

If it compiles fine with gcc, why use g++?

At uni, we compile our C code with:

gcc -Wall source.cpp

mhcox 01-02-2006 08:29 PM

That's the opposite of what I'd expect. The extern "C" is C++ syntax, so gcc should consider it a syntax error. The gcc front end will also compile the file as C++ if the file has a .cc or .cpp extension.

What exactly is the error message?

Mike

lucky6969b 01-02-2006 09:04 PM

Because the large part of our project is written in C++, but there is certain aspect that we could not figure out. So we need to use some C code...
Thanks
Jack

primo 01-02-2006 09:20 PM

Quote:

Originally Posted by mhcox
That's the opposite of what I'd expect. The extern "C" is C++ syntax, so gcc should consider it a syntax error.

There's no problem if you use the __cplusplus macro which is defined only by C++ compilers...

lucky6969b 01-02-2006 09:21 PM

Hi, I see the faulty statement
I defined AC_PROG_CC and AC_PROG_CXX
so some code did not compile
What i want is gcc instead of g++
when I deleted that line, it says C++ code seen, but no AC_PROG_CXX was seen, and told me to add AC_PROG_CXX to configure.in which was not what I wanted.
Thanks
Jack

lucky6969b 01-02-2006 09:26 PM

I am dealing with classes and objects here. Can we use gcc instead of g++?
Thanks
Jack

lucky6969b 01-02-2006 09:34 PM

I wrote a small application that tried to integrate g++ and gcc code together to no avail.

#include <stdio.h>

class hello
{
public:
hello() { }
~hello();

public:
void say_hello(void);
};

void hello::say_hello(void)
{
printf ("Hello\n");
}

int main (int argv, char *argv[])
{
hello h = new hello();
h->say_hello();
return 0;
}
Thanks
Jack

lucky6969b 01-02-2006 09:42 PM

The above code doesn't work with gcc.

lucky6969b 01-02-2006 09:44 PM

When I used gcc for our already-built code, the system forced us to use g++!
:(
Jack

primo 01-02-2006 10:04 PM

Man, gcc does not compile C++ code. Just try to integrate the C code in the C++ compiler.

lucky6969b 01-02-2006 10:07 PM

Thanks primo,
I tried that before
But some APIs like read and write are not recognised by the g++ compiler
Jack

lucky6969b 01-02-2006 10:09 PM

The error mess was
socket.cpp:86: error: 'read' was not declared in this scope

lucky6969b 01-02-2006 10:09 PM

But gcc was okay (as a standalone test)

lucky6969b 01-02-2006 10:10 PM

I meant gcc in native c mode


All times are GMT -5. The time now is 09:00 AM.