Problem : To link a .cpp main file with a C object(.o) and C++ object(.o) using xlc compiler

Problem : To link a .cpp main file with a C object(.o) and C++ object(.o) using xlc compiler

Post by pu.. » Thu, 20 Apr 2000 04:00:00



We are trying to link a .cpp main file with a C object(.o) and C++
object(.o) using xlC compiler on IBM-AIX.

Please let us know, if anyone has a solution to this problem described
below.

Test programs and various options tried :

CASE 1 :

Total 4 Files-
Names   1) com.h
        2) prog5.cpp
        3) prog3.c
        4) main.cpp

/* File com.h           declares class test_c1 */

Thanks

class test_c1{
public:
  test_c1()
  {
     cout << "this  class test";
  }

Quote:};

/* File prog5.c         declares function prog5() */
#include <stdio.h>
#include <string.h>

int prog5()
{
        strlen("abcd");
        printf("prog 5 Hello World !!!\n");
        return(0);

Quote:}

/* File main.cpp                declares class test_c1 */

#include <stdio.h>
#include <iostream.h>
#include "com.h"
extern int prog5(void);
extern void prog3();
void main(void)
{
        test_c1 t2;
        printf("this is a test\n");
        prog5();
        prog3();

Quote:}

/* File prog3.c         declares function prog3() */

#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include "com.h"
int prog3()
{
        test_c1 t1;
        strlen("abcd");
        printf("3 Hello World !!!\n");
        return(0);

Quote:}

Created  objects(.o files) using the following command :
xlC  -c  prog3.cpp
xlC  -c  main.cpp
xlC  -c  prog5.c

Linking the objects(.o files) :

xlC main.o prog3.o prog5,o

This is the error :
ld: 0711-317 ERROR: Undefined symbol: .prog5()
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more
information.

CASE 2 :

Total 3 Files-
Names   1) extern.h
        2) link.c
        3) main.cpp

        File extern.h           declares function cool()

        extern "C"{
                extern void cool(char *a);
        }

        File link.c             defines function cool()

        #include <stdio.h>

        void cool(char *a){
                printf("called\n");
        }

        File main.cpp           //contains main() procedure

        #include <stdio.h>
        #include <string.h>
        #include <sys/malloc.h>

        #include "extern.h"

        void main(void){
                char a[10];
                strcpy(a, "text");
                char *b = (char *)malloc(10);
                cool(a);
                free(b);
        }

Procedures Followed -
     Proc 1
                To create link.o executed command
                        $ cc -c link.c
                To create main.o executed command
                        $ xlC -c main.cpp
                To create executable a.out executed command
                        $ xlC main.o link.o
        Errors -(Screen Capture)
                        $ xlC main.o link.o
                        ld: 0711-317 ERROR: Undefined symbol:
kernel_heap
                        ld: 0711-317 ERROR: Undefined symbol: .xmalloc
                        ld: 0711-317 ERROR: Undefined symbol: .xmfree
                        ld: 0711-345 Use the -bloadmap or -bnoquiet
option to obtain more information.

     Proc 2
                To create link.o executed command
                        $ xlC -c link.c
                To create main.o executed command
                        $ xlC -c main.cpp
                To create executable a.out executed command
                        $ xlC main.o link.o
        Errors -(Screen Capture)
                        $ xlC main.o link.o
                        ld: 0711-317 ERROR: Undefined symbol:
kernel_heap
                        ld: 0711-317 ERROR: Undefined symbol: .xmalloc
                        ld: 0711-317 ERROR: Undefined symbol: .xmfree
                        ld: 0711-345 Use the -bloadmap or -bnoquiet
option to obtain more information.

     Proc 3
                To create link.o executed command
                        $ xlC -+ -c link.c
                To create main.o executed command
                        $ xlC -c  main.cpp
                To create executable a.out executed command
                        $ xlC main.o link.o
        Errors -(Screen Capture)
                        $ xlC main.o link.o
                        ld: 0711-317 ERROR: Undefined symbol:
kernel_heap
                        ld: 0711-317 ERROR: Undefined symbol: .xmalloc
                        ld: 0711-317 ERROR: Undefined symbol: .cool
                        ld: 0711-317 ERROR: Undefined symbol: .xmfree
                        ld: 0711-345 Use the -bloadmap or -bnoquiet
option to obtain more information.

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Problem : To link a .cpp main file with a C object(.o) and C++ object(.o) using xlc compiler

Post by Gary R. Hoo » Fri, 21 Apr 2000 04:00:00


Your code is incorrect.  References to C symbols from C++ must
include a declaration proclaiming them as non-C++ symbols.


> We are trying to link a .cpp main file with a C object(.o) and C++
> object(.o) using xlC compiler on IBM-AIX.

> /* File main.cpp                declares class test_c1 */

> #include <stdio.h>
> #include <iostream.h>
> #include "com.h"
> extern int prog5(void);

This should be:

extern "C" int prog5(void);

Quote:>         File main.cpp           //contains main() procedure

>         #include <stdio.h>
>         #include <string.h>
>         #include <sys/malloc.h>

malloc and friends are declared in the standard #include file stdlib.h,
_not_ sys/malloc.h.  You're creating references to things inside the
kernel
that you don't need.

--
Gary R. Hook / AIX Kernel Development, IBM / These opinions are _MINE_
________________________________________________________________________
A piece of canvas is only the beginning
It takes on character with every loving stroke
This thing of beauty is the passion of an artist's heart
By God's design, we are a skin kaleidoscope    "Colored people", dc Talk

 
 
 

Problem : To link a .cpp main file with a C object(.o) and C++ object(.o) using xlc compiler

Post by pu.. » Mon, 24 Apr 2000 04:00:00


Thank You Very Much for the reply!The suggetions given by you have
worked perfectly.But we still have some problems.

There are some runtime problems while linking the shared library
with our main.cpp.The test programs works fine.

This is the error,we get .
Segmentation fault in __C_runtime_startup at 0x10000340 ($t1)
0x10000340 (__C_runtime_startup+0x128) 807c0004        lwz   r3,0x4(r28)

This error comes only when we link with actual product library.
The actual library very huge and complex ,so trouble shooting
would take some time.

Is it possible for you to throw some light on this ?



> Your code is incorrect.  References to C symbols from C++ must
> include a declaration proclaiming them as non-C++ symbols.


> > We are trying to link a .cpp main file with a C object(.o) and C++
> > object(.o) using xlC compiler on IBM-AIX.

> > /* File main.cpp                declares class test_c1 */

> > #include <stdio.h>
> > #include <iostream.h>
> > #include "com.h"
> > extern int prog5(void);

> This should be:

> extern "C" int prog5(void);

> >         File main.cpp           //contains main() procedure

> >         #include <stdio.h>
> >         #include <string.h>
> >         #include <sys/malloc.h>

> malloc and friends are declared in the standard #include file
stdlib.h,
> _not_ sys/malloc.h.  You're creating references to things inside the
> kernel
> that you don't need.

> --
> Gary R. Hook / AIX Kernel Development, IBM / These opinions are _MINE_

________________________________________________________________________

Quote:> A piece of canvas is only the beginning
> It takes on character with every loving stroke
> This thing of beauty is the passion of an artist's heart
> By God's design, we are a skin kaleidoscope    "Colored people", dc
Talk

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

1. Forte 6.2 C++ object good, lib from object has problems for STL

I'm working with Forte 6.2
xxx.o is an object file that uses some STL (stack which defaults to use deque)
If I link the program with xxx.o it is OK.
I created a library libxxx.a from xxx.o using ar -rc
If I link with  -lxxx I get undefined symbols for some stl stuff (i.e.
std:deque) referenced in that library.  I even did a diff of the symbol tables
and with the exception of the tag identifying the files, they are identical so
I'm inclined to believe this is a care and feeding issue of the 6.2 beast which
I am only using -g -mt options.

Thanks for any suggestions.

(no problem with the GNU port)

Ben

2. EBDA too big

3. how to make one object file from several object files

4. Linux doesn't find my hardware Please Help

5. splitting an object file (.o) into multiple object files

6. mounting root fs while in installition mode

7. Convert SunOS object files into Solaris object files?

8. Finding PCI cards added after boot?

9. Linking Sun4 objects with Solaris objects?

10. automake/autoconf: linking object files built with different compilers

11. C and C++, linking object files

12. Linking C and C++ code object files with 'ld'?

13. Linking mixed objects from g++ and xlC