问题
why am i getting this error?
g++ -m32 func.o test.o -o prgram
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status
my code is as follows:
#include<stdio.h>
2
3
4 extern "C" int calcsum(int a, int b, int c);
5
6 int main(int argc,char* argv[])
7 {
8 int a = 10;
9 int b = 20;
10 int c = 30;
11 int sum = calcsum(a,b,c);
12
13 printf("a: %d", a);
14 printf("b: %d", b);
15 printf("c: %d", c);
16 printf("sum: %d", sum);
17 return 0;
18 }
19
1 section .text
2 global _start
3 _start:
4 global _calcsum
5 _calcsum:
6
7 push ebp
8 mov ebp, esp
9
10 mov eax, [ebp+8]
11 mov ecx, [ebp+12]
12 mov edx, [ebp+16]
13
14 add eax, ecx
15 add eax, edx
16
17 pop ebp
18 ret
回答1:
You are building a 32-bit binary on a 64-bit system (because you used the -m32
flag), but have not installed 32-bit versions of the development libraries.
If you're on Ubuntu, try:
sudo apt install gcc-multilib g++-multilib libc6-dev-i386
As for your assembly file, it can't work as-is. start
will be defined by the cpp file, so remove it from the asm file. Next, remove the underscore from _calcsum
. When building ELF binaries, function names do not start with an underscore:
section .text
global calcsum
calcsum:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov ecx, [ebp+12]
mov edx, [ebp+16]
add eax, ecx
add eax, edx
pop ebp
ret
Build that as a 32-bit ELF object file with:
nasm -felf32 func.s -o func.o
来源:https://stackoverflow.com/questions/60553541/usr-bin-ld-skipping-incompatible-usr-lib-gcc-x86-64-linux-gnu-9-libstdc-a-w