compiling C code using aCC on HPUX

W

wenmang

Hi,
We have the 3rd party libraries written in C. And, in every header
file from 3rd party, it has the include guarde:
#ifdef __cplusplus
#define extern "C" {
:
APIs...
:
}
#endif
:
:
To me, it is clear that the 3rd party libraries must be compiled using
C and C++ compiler should not do anything about them and linker should
take care of them at link time.

Here is my question:
Could I compile C++ program using those C APIs? What I mean here is: I
want to use aCC(HPUX) to compile everything and treat all 3rd party C
APIs as regular C functions, and I failed.

If 3rd party header files don't have extern "C" include guard, could I
succeed?
If I want to succeed, I should using C++ compiler to recompile all 3rd
party libraries(C codes), is this a correct statement?
 
E

E. Robert Tisdale

wenmang said:
We have the 3rd party libraries written in C.
And every header file from 3rd party has the guard:

#ifdef __cplusplus
#define extern "C" {
:
APIs...
:
}
#endif
:

To me, it is clear that the 3rd party libraries must be compiled using
C and C++ compiler should not do anything about them and linker should
take care of them at link time.

Here is my question:
Could I compile C++ program using those C APIs?
What I mean here is: I want to use aCC(HPUX) to compile everything
and treat all 3rd party C APIs as regular C functions but I failed.

If 3rd party header files don't have extern "C" guard,
could I succeed?
If I want to succeed, I should using C++ compiler to recompile all 3rd
party libraries(C codes), is this a correct statement?

You are confused.
The third party header files contain something like this:

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
/* function declarations*/
#ifdef __cplusplus
}
#endif/*__cplusplus */

The third party source files should contain something like this:

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
/* function definitions */
#ifdef __cplusplus
}
#endif/*__cplusplus */

Your C++ compiler may actually call the C compiler
if the source files have a .c extension.
Consult the man page for your C++ compiler
to find the option that overrides this behavior.

There is no guarantee that functions compiled by a C compiler
(or even another C++ compiler for that matter)
can be called by programs compiled by your C++ compiler.
It is usually best to recompile everything with your C++ compiler
if you have all of the source code.
 
W

wenmang

Thanks. My confusion is that we don't have 3rd party source codes but
the binary libraries.
I try to create some C++ components implemented by 3rd party APIs. The
source files are .cpp
files and aCC is used to compile the code. I try to understand that if
the c++ source code is using 3rd party APIs, how does C++ compiler
treat those APIs or maybe it is the linker's job?
 
W

wenmang

When I try to compile the Cpp code that calls 3rd party APIs, C++
compiler complains that the 3rd APIs called have only 0 argument, but
actually, they have more than 1 arguments. I guess that the APIs
declaration/defination is not properly "included" into the C++ code,
how do I fix it?
Thanks.
 
R

Ron Natalie

wenmang said:
When I try to compile the Cpp code that calls 3rd party APIs, C++
compiler complains that the 3rd APIs called have only 0 argument, but
actually, they have more than 1 arguments. I guess that the APIs
declaration/defination is not properly "included" into the C++ code,
how do I fix it?

Most likely your "Third Party" API's are C functions that do not have full
(in C terms) prototypes. For example a function:

int ThirdPartyAPI(int);

in C is validly declared as

int ThirdPartyAPI();

(this is the way C was originally, function argument declarations came later).

However, this won't work in C++. If you want to do this, you're going to have
to replace these with proper prototypes (C++ declarations). In addition, you're
likely to need to declare them as extern "C".
 
E

E. Robert Tisdale

Ron said:
Most likely your "Third Party" API's are C functions that do not have full
(in C terms) prototypes. For example a function:

int ThirdPartyAPI(int);

in C is validly declared as

int ThirdPartyAPI();

(this is the way C was originally, function argument declarations came later).

However, this won't work in C++. If you want to do this,
you must to replace these with proper prototypes (C++ declarations).
In addition, you're likely to need to declare them as extern "C".

No!

extern "C" int ThirdPartyAPI(int);

and

extern "C" int ThirdPartyAPI();

have the *same* external linkage.
 
R

Ron Natalie

E. Robert Tisdale said:
Yes.

extern "C" int ThirdPartyAPI(int);

and

extern "C" int ThirdPartyAPI();

have the *same* external linkage.

They have the same linkage, but the definitions are DIFFERENT. The latter
is a function taking no arguments. Changing the linkage doesn't change
the fact that this is still C++, you must specify the arguments (or use ...).
 
E

E. Robert Tisdale

Ron said:
They have the same linkage, but the definitions are DIFFERENT. The latter
is a function taking no arguments. Changing the linkage doesn't change
the fact that this is still C++, you must specify the arguments (or use ...).

Yes.

But I'm not sure that we are any closer to solving Wenmang's problem.
 
W

wenmang

Thanks for the help. I found the problem. The 3rd party library uses
"#define" to check whether it is "_ANSI_C_SOURCE", if yes, it defines
function prototype as int API(arg,...), if it is MS window system, it
defines function prototype as int API(). I don't know why, but it is
what happens. I just define "_ANSI_C_SOURCE" in my code before include
any 3rd party's header file, that solves my problem, thanks.
 

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,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top