Compiling C into C++

J

jois.de.vivre

I'm trying to include a C header file from an external library in my C+
+ program but when I compile I get the following error:

error: expected unqualified-id before 'private'
error: abstract declarator 'void*' used as declaration
error: expected ';' before 'private'

It didn't take me long to figure out that the offending C header file
uses a variable with the name "private". I've reproduced the problem
as follows:

/* -------- a.h - the C header file */
#ifndef TEST_H
#define TEST_H

struct s
{
void *private;
};

#endif
/* -------- end a.h */


/* -------- b.cpp - the C++ header file trying to include a.h */
extern "C" {
#include "a.h"
}
/* -------- end b.cpp */

I am compiling using this command:

%> g++ -o b.o b.cpp

Despite enclosing the inclusion of the header file with extern "C",
the problem persists. As far as I know, 'private' is not a C key word
(confirmed by the fact that it compiles with the C compiler).

What am I doing wrong here?
 
J

Jim Langston

I'm trying to include a C header file from an external library in my C+
+ program but when I compile I get the following error:

error: expected unqualified-id before 'private'
error: abstract declarator 'void*' used as declaration
error: expected ';' before 'private'

It didn't take me long to figure out that the offending C header file
uses a variable with the name "private". I've reproduced the problem
as follows:

/* -------- a.h - the C header file */
#ifndef TEST_H
#define TEST_H

struct s
{
void *private;
};

#endif
/* -------- end a.h */


/* -------- b.cpp - the C++ header file trying to include a.h */
extern "C" {
#include "a.h"
}
/* -------- end b.cpp */

I am compiling using this command:

%> g++ -o b.o b.cpp

Despite enclosing the inclusion of the header file with extern "C",
the problem persists. As far as I know, 'private' is not a C key word
(confirmed by the fact that it compiles with the C compiler).

What am I doing wrong here?

Well, I don't think you're doing anything wrong, it's just that private
became a keyword in C++. What I would probably do is change the variable
name of private in the header file to something else. Something like:

#ifndef TEST_H
#define TEST_H

#define private priv

// file here

#undef private
#endif

This should have the effect of changing all occurances of "private" in that
header to "priv" wihtout having to modify the header file beyond that two
precompiler directives.
 
M

mlimber

I'm trying to include a C header file from an external library in my C+
+ program but when I compile I get the following error:

error: expected unqualified-id before 'private'
error: abstract declarator 'void*' used as declaration
error: expected ';' before 'private'

It didn't take me long to figure out that the offending C header file
uses a variable with the name "private". I've reproduced the problem
as follows:

/* -------- a.h - the C header file */
#ifndef TEST_H
#define TEST_H

struct s
{
void *private;

};

#endif
/* -------- end a.h */

/* -------- b.cpp - the C++ header file trying to include a.h */
extern "C" {
#include "a.h"}

/* -------- end b.cpp */

I am compiling using this command:

%> g++ -o b.o b.cpp

Despite enclosing the inclusion of the header file with extern "C",
the problem persists. As far as I know, 'private' is not a C key word
(confirmed by the fact that it compiles with the C compiler).

What am I doing wrong here?

private is a C++ keyword, and you're building with a C++ compiler (the
extern "C" only affects the linkage, not the language syntax that is
permissible). It would not be any better if an identifier were named
"class", "template", or any of the other keywords that C++ has that C
does not. You need to modify the header, or you can't use it with C++
code.

Cheers! --M
 
F

Fei Liu

I'm trying to include a C header file from an external library in my C+
+ program but when I compile I get the following error:

error: expected unqualified-id before 'private'
error: abstract declarator 'void*' used as declaration
error: expected ';' before 'private'

It didn't take me long to figure out that the offending C header file
uses a variable with the name "private". I've reproduced the problem
as follows:

/* -------- a.h - the C header file */
#ifndef TEST_H
#define TEST_H

struct s
{
void *private;
};

#endif
/* -------- end a.h */


/* -------- b.cpp - the C++ header file trying to include a.h */
extern "C" {
#include "a.h"
}
/* -------- end b.cpp */

I am compiling using this command:

%> g++ -o b.o b.cpp

Despite enclosing the inclusion of the header file with extern "C",
the problem persists. As far as I know, 'private' is not a C key word
(confirmed by the fact that it compiles with the C compiler).

What am I doing wrong here?

private is a C++ keyword. You cannot use it as a class member name.

Fei
 
A

Anand Hariharan

I'm trying to include a C header file from an external library in my C+
+ program but when I compile I get the following error:

error: expected unqualified-id before 'private'
error: abstract declarator 'void*' used as declaration
error: expected ';' before 'private'

It didn't take me long to figure out that the offending C header file
uses a variable with the name "private". (...)

Despite enclosing the inclusion of the header file with extern "C",
the problem persists. As far as I know, 'private' is not a C key word
(confirmed by the fact that it compiles with the C compiler).

What am I doing wrong here?

'extern "C"' does not mean "treat the following code as C even though
it is within a C++ program". It means that "make the following code
available to C code even though it is within C++ code".

- Anand
 
G

Greg Comeau

I'm trying to include a C header file from an external library in my C+
+ program but when I compile I get the following error:

error: expected unqualified-id before 'private'
error: abstract declarator 'void*' used as declaration
error: expected ';' before 'private'

It didn't take me long to figure out that the offending C header file
uses a variable with the name "private". I've reproduced the problem
as follows:

/* -------- a.h - the C header file */
#ifndef TEST_H
#define TEST_H

struct s
{
void *private;
};

#endif
/* -------- end a.h */


/* -------- b.cpp - the C++ header file trying to include a.h */
extern "C" {
#include "a.h"
}
/* -------- end b.cpp */

I am compiling using this command:

%> g++ -o b.o b.cpp

Despite enclosing the inclusion of the header file with extern "C",
the problem persists. As far as I know, 'private' is not a C key word
(confirmed by the fact that it compiles with the C compiler).

What am I doing wrong here?

extern "C" does not mean that from that point the C++ compiler
acts as a C compiler. It's still C++. So while it's true that
C does not support a private keyword, C++ does, hence that
keyword still exists when compiling as C++. As it should.
Perhaps this will help in some way:

http://www.comeaucomputing.com/techtalk/#externc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top