gcc と cc のオブジェクト互換の実験2009年03月12日 00時48分35秒

G++ 拡張を使っているプログラムがあったため、 Sun の CC ではコンパイル出来ない物があった。Sun の C コンパイラで作ったオブジェクトと GCC コンパイラで作ったオブジェクトに互換性があったか思い出せないので簡単に実験してみた。

環境は、Solaris 8。


% uname -a
SunOS hostname 5.8 Generic_117350-25 sun4u sparc SUNW,Sun-Fire-V890

入っていたコンパイラは以下の通り。


% CC -V
CC: Sun WorkShop 6 update 1 C++ 5.2 2000/09/11
% g++ -v
Reading specs from /apps/local/bin/../lib/gcc/sparc-sun-solaris2.8/3.4.6/specs
Configured with: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/l
d --enable-shared --enable-languages=c,c++,f77
Thread model: posix
gcc version 3.4.6

興味あるのは C++ のコードなのだが、C で互換性がなかったら、C++ では無理だろうと予想して、まずは C で実験した。


% cat a.c
int func()
{
        return 1;
}
% cat b.c
#include 

extern int func();

int main()
{
        printf("%d\n", func());
        return;
}

片方ずつ、コンパイラを変えて試す。


% CC -c a.c
% gcc -c -o b.o b.c

a.c を Sun C で、b.c を GCC でコンパイル。

% ld -o mix a.o b.o
Undefined                       first referenced
 symbol                             in file
func                                b.o
ld: fatal: Symbol referencing errors. No output written to mix

リンクに失敗した。折角なので、nm で覗いてみる。

% nm a.o


a.o:

[Index]   Value      Size    Type  Bind  Other Shndx   Name

[2]     |        16|      36|FUNC |GLOB |0    |2      |__1cEfunc6F_i_
[1]     |         0|       0|FILE |LOCL |0    |ABS    |a.c
% nm b.o


b.o:

[Index]   Value      Size    Type  Bind  Other Shndx   Name

[2]     |         0|       0|SECT |LOCL |0    |3      |
[3]     |         0|       0|SECT |LOCL |0    |2      |
[1]     |         0|       0|FILE |LOCL |0    |ABS    |b.c
[6]     |         0|       0|NOTY |GLOB |0    |UNDEF  |func
[5]     |         0|      52|FUNC |GLOB |0    |3      |main
[4]     |         0|       0|NOTY |GLOB |0    |UNDEF  |printf

今度は逆にしてみたが、やはり駄目だった。


% gcc -c -o a.o a.c
% CC -c -o b.o b.c
% ld -o mix a.o b.o
Undefined                       first referenced
 symbol                             in file
__1cEfunc6F_i_                      b.o
ld: fatal: Symbol referencing errors. No output written to mix

こちらでも、nm でシンボルを調べてみる。

% nm a.o


a.o:

[Index]   Value      Size    Type  Bind  Other Shndx   Name

[2]     |         0|       0|SECT |LOCL |0    |2      |
[1]     |         0|       0|FILE |LOCL |0    |ABS    |a.c
[3]     |         0|      20|FUNC |GLOB |0    |2      |func
% nm b.o


b.o:

[Index]   Value      Size    Type  Bind  Other Shndx   Name

[2]     |         0|       0|SECT |LOCL |0    |3      |
[3]     |         0|       0|NOTY |GLOB |0    |UNDEF  |__1cEfunc6F_i_
[6]     |         0|       0|NOTY |GLOB |0    |ABS    |__fsr_init_value
[1]     |         0|       0|FILE |LOCL |0    |ABS    |b.c
[5]     |        16|      60|FUNC |GLOB |0    |2      |main
[4]     |         0|       0|NOTY |GLOB |0    |UNDEF  |printf