Linking .o from C++ and C 
Author Message
 Linking .o from C++ and C

I have some C++ code that is automatically generated which I want to
compile and link into my C application.  I find that I can compile it
immediately with: gcc -c foo.cpp -o foo.o
but when I link foo.o I get a linker error - undefined reference to a
function in foo.cpp.  If I copy foo.cpp to foo.c and make a couple of
small changes it compiles as a C program then links OK.  I'd rather not
have to make these manual modifications, and I'm guessing that there is
a way to make the foo.o built from foo.cpp acceptable to the linker.
Can someone enlighten me?

BTW, the two things that give compiler errors with .c but not .cpp are:

1) A structure definition:

struct MATERIAL {
  float x[3];

Quote:
};

static MATERIAL materials[1] = {1.0, 2.0, 3.0 };

I need to change the definition to a typdef:

struct MATERIAL {
  float x[3];

Quote:
} MATERIAL;

2) Instruction ordering:

void myfun(void)
{
  fun1();
  float alpha=materials[1].x[0];
  ...

I need to swap these two lines.

thanks
Gib



Thu, 03 Feb 2005 11:07:06 GMT  
 Linking .o from C++ and C

in comp.lang.c:

Quote:
> I have some C++ code that is automatically generated which I want to
> compile and link into my C application.  I find that I can compile it
> immediately with: gcc -c foo.cpp -o foo.o
> but when I link foo.o I get a linker error - undefined reference to a
> function in foo.cpp.  If I copy foo.cpp to foo.c and make a couple of
> small changes it compiles as a C program then links OK.  I'd rather not
> have to make these manual modifications, and I'm guessing that there is
> a way to make the foo.o built from foo.cpp acceptable to the linker.
> Can someone enlighten me?

> BTW, the two things that give compiler errors with .c but not .cpp are:

> 1) A structure definition:

> struct MATERIAL {
>   float x[3];
> };

> static MATERIAL materials[1] = {1.0, 2.0, 3.0 };

> I need to change the definition to a typdef:

> struct MATERIAL {
>   float x[3];
> } MATERIAL;

> 2) Instruction ordering:

> void myfun(void)
> {
>   fun1();
>   float alpha=materials[1].x[0];
>   ...

> I need to swap these two lines.

> thanks
> Gib

The C language does not define an interface to any other language, at
all, including C++.  You need to take this to a compiler specific
group.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Thu, 03 Feb 2005 11:46:15 GMT  
 Linking .o from C++ and C

Quote:
> I have some C++ code that is automatically generated which I want to
> compile and link into my C application.  I find that I can compile it
> immediately with: gcc -c foo.cpp -o foo.o
> but when I link foo.o I get a linker error - undefined reference to a
> function in foo.cpp.  If I copy foo.cpp to foo.c and make a couple of
> small changes it compiles as a C program then links OK.  I'd rather not
> have to make these manual modifications, and I'm guessing that there is
> a way to make the foo.o built from foo.cpp acceptable to the linker.
> Can someone enlighten me?

> BTW, the two things that give compiler errors with .c but not .cpp are:

> 1) A structure definition:

> struct MATERIAL {
>   float x[3];
> };

> static MATERIAL materials[1] = {1.0, 2.0, 3.0 };

> I need to change the definition to a typdef:

> struct MATERIAL {
>   float x[3];
> } MATERIAL;

> 2) Instruction ordering:

> void myfun(void)
> {
>   fun1();
>   float alpha=materials[1].x[0];
>   ...

> I need to swap these two lines.

The second problem is not a problem if you have a C99 compiler. The first
problem is one which you know how to resolve. Although linking is an
integral part of C programming in the real world, the C standard does not
specify how a C implementation is to implement the linking of different
translation units, let alone object files compiled from other languages. So
really, there's nothing topical left for comp.lang.c to answer in your post.

But the way I see it, you could use a C99 compiler [or a compiler
extension - please consult appropriate compiler newsgroup] to fix problem 2,
and wrap the pseudo C++ file...

  /* foo.c: compiled instead of foo.cpp itself */
  typedef struct MATERIAL MATERIAL;
  #include "foo.cpp"

That said, I'd actually be more inclined to continue to compile the code
with a C++ compiler. In which case, you should take you question to a C++
group which can explain how to make C++ functions visible to C functions.

--
Peter



Thu, 03 Feb 2005 12:03:53 GMT  
 Linking .o from C++ and C

Quote:


> in comp.lang.c:

> > I have some C++ code that is automatically generated which I want to
> > compile and link into my C application.  I find that I can compile it
> > immediately with: gcc -c foo.cpp -o foo.o
> > but when I link foo.o I get a linker error - undefined reference to a
> > function in foo.cpp.  If I copy foo.cpp to foo.c and make a couple of
> > small changes it compiles as a C program then links OK.  I'd rather not
> > have to make these manual modifications, and I'm guessing that there is
> > a way to make the foo.o built from foo.cpp acceptable to the linker.
> > Can someone enlighten me?

> > BTW, the two things that give compiler errors with .c but not .cpp are:

> > 1) A structure definition:

> > struct MATERIAL {
> >   float x[3];
> > };

> > static MATERIAL materials[1] = {1.0, 2.0, 3.0 };

> > I need to change the definition to a typdef:

> > struct MATERIAL {
> >   float x[3];
> > } MATERIAL;

> > 2) Instruction ordering:

> > void myfun(void)
> > {
> >   fun1();
> >   float alpha=materials[1].x[0];
> >   ...

> > I need to swap these two lines.

> > thanks
> > Gib

> The C language does not define an interface to any other language, at
> all, including C++.  You need to take this to a compiler specific
> group.

Would it be too much to ask which newsgroup?


Thu, 03 Feb 2005 16:17:19 GMT  
 Linking .o from C++ and C

Quote:


>> The C language does not define an interface to any other language, at
>> all, including C++.  You need to take this to a compiler specific
>> group.
>Would it be too much to ask which newsgroup?

Since you want to link intermediate files generated from _two_ different
languages, and since exactly _one_ of them does not define an interface
to the other one, the choice in the comp.lang hierarchy should not
be too difficult.

If there are compiler specific details that are difficult to solve,
finding out what compiler you use should be the first step in your
quest.

Kurt

--
| Kurt Watzka                            



Fri, 04 Feb 2005 00:19:14 GMT  
 Linking .o from C++ and C

Quote:



> >> The C language does not define an interface to any other language, at
> >> all, including C++.  You need to take this to a compiler specific
> >> group.

> >Would it be too much to ask which newsgroup?

> Since you want to link intermediate files generated from _two_ different
> languages, and since exactly _one_ of them does not define an interface
> to the other one, the choice in the comp.lang hierarchy should not
> be too difficult.

> If there are compiler specific details that are difficult to solve,
> finding out what compiler you use should be the first step in your
> quest.

One of the interesting characteristics of this newsgroup, compared with
several others that I frequent, is how many people here can't resist
making smart comments/put-downs.


Fri, 04 Feb 2005 05:40:56 GMT  
 Linking .o from C++ and C
On Sun, 18 Aug 2002 14:03:53 +1000, in comp.lang.c , "Peter Nilsson"

Quote:

>  /* foo.c: compiled instead of foo.cpp itself */
>  typedef struct MATERIAL MATERIAL;
>  #include "foo.cpp"

this is a VERY bad idea - #including source code is strongly
disrecommended, no matter what bad practices MS may demonstrate.

Quote:
>That said, I'd actually be more inclined to continue to compile the code
>with a C++ compiler.

*phew*, thank goodness!

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Fri, 04 Feb 2005 06:17:35 GMT  
 Linking .o from C++ and C

Quote:




<snip>

> > If there are compiler specific details that are difficult to solve,
> > finding out what compiler you use should be the first step in your
> > quest.

> One of the interesting characteristics of this newsgroup, compared with
> several others that I frequent, is how many people here can't resist
> making smart comments/put-downs.

Another characteristic of this newsgroup, just as interesting in its own
way, is how many people *do* resist making smart comments/put-downs
*most* of the time, almost irrespective of the temptation and
opportunity so to do. Sometimes you can almost /taste/ the one-liner
that was *nearly* written, but where the respondent mercifully stayed
his hand at the last.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 04 Feb 2005 06:17:57 GMT  
 Linking .o from C++ and C

Quote:

> On Sun, 18 Aug 2002 14:03:53 +1000, in comp.lang.c , "Peter Nilsson"

> >  /* foo.c: compiled instead of foo.cpp itself */
> >  typedef struct MATERIAL MATERIAL;
> >  #include "foo.cpp"

> this is a VERY bad idea - #including source code is strongly disrecommended,

What else could ever be #included? ;-)

Jirka



Fri, 04 Feb 2005 10:32:31 GMT  
 Linking .o from C++ and C
On Mon, 19 Aug 2002 04:32:31 +0200, in comp.lang.c , Jirka Klaue

Quote:


>> On Sun, 18 Aug 2002 14:03:53 +1000, in comp.lang.c , "Peter Nilsson"

>> >  /* foo.c: compiled instead of foo.cpp itself */
>> >  typedef struct MATERIAL MATERIAL;
>> >  #include "foo.cpp"

>> this is a VERY bad idea - #including source code is strongly disrecommended,

>What else could ever be #included? ;-)

grin. either
        s/source/executable/
or
        code=executable stuff

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Sat, 05 Feb 2005 06:52:31 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. Need C++ text for non cs major course

3. How to show/call Form2.cs from Form1.cs ?

4. Include code in other Cs files

5. Reuse of cs files, namespace, arch advice pls

6. word - automatic numbering/bold/underline/italics

7. How to Generate .cs file at Runtime

8. newbe/cs student, need help w/ code

9. Serial.cs

10. Compile CS source code using ICodeCompiler

11. Two CS files (using namespaces)

12. My .cs files were deleted!?

 

 
Powered by phpBB® Forum Software