🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Max Welton Mon, 27 Nov 1995 19:46
Problems #including K&R function prototypes from C++
#845
Author: Max Welton
Date: Mon, 27 Nov 1995 19:46
50 lines
1749 bytes
I'm attempting to use g++ 2.7.1 with a combination of 
our own C++ and various vendor's C. When a #included
header contains a K&R style function prototype, g++ 
is unable to parse it (see below). The HP C++ cfront
compiler handles the same code with a warning (again, 
see below).

We have found nothing in the gcc/g++ man-pages that
addresses this problem and are not in a position to
edit the third-party header files. We would very much
like to use g++ (for various reasons). Have I overlooked
something obvious?

$ cat foo.h
#ifndef foo_h
#define foo_h
int foo(boo)
   int boo;
{
   return boo;   
}
#endif

$ cat main.C
#include <iostream.h>
extern "C" {
#include "foo.h"
}
int main()
{
   cout << "Hello" << endl;
   return foo(0);
}

$ g++ -c main.C
main.C:4: `boo' was not declared in this scope
main.C:5: parse error before `int'
main.C:6: parse error before `{'

Compilation exited abnormally with code 1 at Mon Nov 27 11:26:24

CC -c main.C
CC: "main.C", line 4: warning: old style definition of foo() (anachronism)
(1386)

Compilation finished at Mon Nov 27 11:35:38
--
Max Welton, CMST (Coordinator of Many Small Things)
   maxw@lmsc.lockheed.com
   maxw@ix.netcom.com
Re: Problems #including K&R function prototypes from C++
#846
Author: Shekar Ramanathan
Date: Mon, 27 Nov 1995 21:23
60 lines
1851 bytes
Max Welton (maxw@lmsc.lockheed.com) wrote:
[snip]
> We have found nothing in the gcc/g++ man-pages that
> addresses this problem and are not in a position to
> edit the third-party header files. We would very much
> like to use g++ (for various reasons). Have I overlooked
> something obvious?

Yes.  You should have only function *prototypes*, not function definitions,
inside extern "C" (see FAQ).

> $ cat foo.h
> #ifndef foo_h
> #define foo_h
> int foo(boo)
>    int boo;
> {
>    return boo;   
> }
> #endif

> $ cat main.C
> #include <iostream.h>
> extern "C" {
> #include "foo.h"
> }
> int main()
> {
>    cout << "Hello" << endl;
>    return foo(0);
> }

What you should do is have another file called foo.c and compile it using
a K&R C compiler.  In main.C, you should just include a prototype declaration
for foo and finally link them together.

foo.c
int foo(boo)
    int boo;
{
   return boo;   
}

main.C:

extern "C"{
	int foo();
}
int main()
{ cout << foo(0) << endl; }


Shekar

--
---------------------------------------------------------------------
Shekar Ramanathan              |   mailto:shekar@CS.MsState.edu
Department of Computer Science |   http://www.cs.msstate.edu/~shekar
Mississippi State University   |
---------------------------------------------------------------------
Re: Problems #including K&R function prototypes from C++
#841
Author: Max Welton
Date: Tue, 28 Nov 1995 00:11
44 lines
1695 bytes
shekar@gizmo.cs.msstate.edu (Shekar Ramanathan) wrote:
>Max Welton (maxw@lmsc.lockheed.com) wrote:
[snip]
>What you should do is have another file called foo.c and compile it using
>a K&R C compiler.  In main.C, you should just include a prototype declaration
>for foo and finally link them together.
>
>foo.c
>int foo(boo)
>    int boo;
>{
>   return boo;   
>}
>
>main.C:
>
>extern "C"{
>	int foo();
>}
>int main()
>{ cout << foo(0) << endl; }
>
>
>Shekar

I have the same problem with or without the 'extern"C"'. In the 
real-world case, "foo.h" is a 3rd-party vendor header. We're just 
not in a position to rewrite our favorite vendor's K&R headers.

I was hoping that g++ could be cajoled somehow into the behaviour 
of our HP cfront compiler in this case:

  CC -c main.C
  CC: "main.C", line 4: warning: old style definition of foo() (anachronism) 
  (1386)

  Compilation finished at Mon Nov 27 11:35:38

Not there yet...

--
Max Welton, CMST (Coordinator of Many Small Things)
   maxw@lmsc.lockheed.com
   maxw@ix.netcom.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads