我试图在Ubuntu中使用随机函数在更大的程序上使用g ++进行编译,出于某种原因,rand只是给出了奇怪的编译错误。出于测试目的,我制作了最简单的程序,它仍然会出错。
程序:
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
int main()
{
cout << "Random number " << rand();
return 0;
}
sudo g++ chapter_3/tester.cpp ./test
./test: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:65: multiple definition of `_start'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf /start.S:65: first defined here
./test:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata+0x0): first defined here
./test: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.fini+0x0): first defined here
./test:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
./test: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o:(.data+0x0): first defined here
./test: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i486-linux-gnu/4.4.1/crtbegin.o:(.data+0x0): first defined here
./test: In function `main':
(.text+0xb4): multiple definition of `main'
/tmp/cceF0x0p.o:tester.cpp:(.text+0x0): first defined here
./test: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i486-linux-gnu/4.4.1/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
./test:(.dtors+0x4): first defined here
/usr/bin/ld: error in ./test(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
答案 0 :(得分:2)
您似乎将文件链接到自身,从而导致生成的可执行文件中的符号有多个定义。试试这个:
g++ chapter_3/tester.cpp
也不要将sudo用于此类命令。