I am trying out some code on Ubuntu. I'm trying to run the following code
我正在试用Ubuntu的一些代码。我正在尝试运行以下代码。
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "random.h"
using namespace std;
/* Function prototype! */
void initRandomSeed();
int randomInteger(int low,int high){
initRandomSeed();
double d= rand()/(double(RAND_MAX)+1);
double s= d*(double(high)-low+1);
return int(floor(low)+s);
}
double randomReal(int low,int high){
initRandomSeed();
double d=rand()/(double(RAND_MAX)+1);
double s=d*(double(high)-low+1);
return low+s;
}
bool randomChance(double p){
initRandomSeed();
return randomReal(0,1)<p;
}
void setRandomSeed(int seed){
initRandomSeed();
srand(seed);
}
void initRandomSeed(){
// to retain updated values across different stack frames! nice!
static bool initialized=false;
// this is executed only very first time and random value obtained from system clock!
if(!initialized){
srand(int(time(NULL)));
initialized=true;
}
}
And when I try to compile the above code using g++
, I get the following error
当我尝试用g++编译上面的代码时,我得到了以下错误。
@ubuntu:~/Chardway$ g++ random.cpp
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
Any help or links to questions that help would be really helpful! Thanks!
任何帮助或联系的问题,帮助将是非常有帮助的!谢谢!
5 个解决方案
#1
97
I'm not sure about your invalid relocation errors but the obvious thing missing is that you have no main
function. You need to define an entry point to your application called main
, defined at global scope such as:
我不确定您的迁移错误是否无效,但是明显缺少的是您没有主函数。您需要为应用程序定义一个名为main的入口点,它在全局范围内定义如下:
int main()
{
// TODO: implementation
}
#2
11
The "undefined reference to 'main'" is because you did not define a main()
function, which is the entry point of your program:
“main”的未定义引用是因为您没有定义main()函数,它是程序的入口点:
int main()
{
// call other functions
}
#3
7
Interestingly, I get the same error if I try to compile a .h
file instead of a .c
file, and link against a library, all in one step.
有趣的是,如果我尝试编译一个.h文件而不是一个.c文件,并链接到一个库,那么我将得到相同的错误。
Here is a greatly reduced example:
这里有一个大大简化的例子:
$ echo 'int main () {}' > test.h
$ g++ test.h -ltommath && echo success
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
In this case, the solution is to rename the file to end with .c
:
在这种情况下,解决方案是将文件重命名为.c:
$ echo 'int main () {}' > test.c
$ g++ test.c -ltommath && echo success
success
#4
2
I just faced this same thing when linking in gtest with CMake and including a file that included a main function.
我只是在gtest和CMake链接时遇到了同样的问题,包括一个包含main函数的文件。
So, if you're sure you have a main, and you're linking something -- make sure you don't have two int main()
s!
所以,如果你确定你有一个main,并且你正在连接某些东西——确保你没有两个int main() !
Simple solution was to split the main() into main.cpp and not link it with the test sources.
简单的解决方案是将main()分解为main。cpp与测试源无关。
#5
-5
You have typed wrong command for g++. You should have typed something like:
您键入错误的命令,用于g++。你应该输入如下内容:
g++ file_name random.cpp
You need to name output file. Otherwise it's like "g++ syntax error".
您需要命名输出文件。否则就像“g++语法错误”。
#1
97
I'm not sure about your invalid relocation errors but the obvious thing missing is that you have no main
function. You need to define an entry point to your application called main
, defined at global scope such as:
我不确定您的迁移错误是否无效,但是明显缺少的是您没有主函数。您需要为应用程序定义一个名为main的入口点,它在全局范围内定义如下:
int main()
{
// TODO: implementation
}
#2
11
The "undefined reference to 'main'" is because you did not define a main()
function, which is the entry point of your program:
“main”的未定义引用是因为您没有定义main()函数,它是程序的入口点:
int main()
{
// call other functions
}
#3
7
Interestingly, I get the same error if I try to compile a .h
file instead of a .c
file, and link against a library, all in one step.
有趣的是,如果我尝试编译一个.h文件而不是一个.c文件,并链接到一个库,那么我将得到相同的错误。
Here is a greatly reduced example:
这里有一个大大简化的例子:
$ echo 'int main () {}' > test.h
$ g++ test.h -ltommath && echo success
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
In this case, the solution is to rename the file to end with .c
:
在这种情况下,解决方案是将文件重命名为.c:
$ echo 'int main () {}' > test.c
$ g++ test.c -ltommath && echo success
success
#4
2
I just faced this same thing when linking in gtest with CMake and including a file that included a main function.
我只是在gtest和CMake链接时遇到了同样的问题,包括一个包含main函数的文件。
So, if you're sure you have a main, and you're linking something -- make sure you don't have two int main()
s!
所以,如果你确定你有一个main,并且你正在连接某些东西——确保你没有两个int main() !
Simple solution was to split the main() into main.cpp and not link it with the test sources.
简单的解决方案是将main()分解为main。cpp与测试源无关。
#5
-5
You have typed wrong command for g++. You should have typed something like:
您键入错误的命令,用于g++。你应该输入如下内容:
g++ file_name random.cpp
You need to name output file. Otherwise it's like "g++ syntax error".
您需要命名输出文件。否则就像“g++语法错误”。