g++ undefined reference to `main'

今天我在尝试使用g++编译程序,但是遇到了一些挫折
是这样:
1.cpp

#include <iostream>
extern int a;
void print()
{
        std::cout << a;
}

2.cpp

int a = 3;

3.cpp

#include <iostream>
void print();
int main()
{
        extern int a;
        print();
        std::cout << std::endl;
        std::cout << a << std::endl;
}

我是想测试一下这个extern 关键字的用法。
我写的makefile文件是这样:

extern_test: 3.o
1.o: 1.cpp 2.cpp
2.o: 2.cpp
3.o: 1.o 3.cpp
clean: rm  1.o 2.o 3.o

在3.cpp中我想测试:
1.打印extern int a的值,即2.cpp中a的值
2.打印print函数中的值,即1.cpp中函数print的值
但是执行:make 命令后
结果是:

xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ make
g++    -c -o 1.o 1.cpp
g++    -c -o 3.o 3.cpp

只生成了两个.o文件,执行./3.o文件

xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ ./3.o
bash: ./3.o: cannot execute binary file: Exec format error

.o文件只是编译后的二进制文件,还没有进行链接。
我又手动编译了一下:

xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ g++ -o 1.o 1.cpp 2.cpp

然后报错:

......
/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

那么如何处理这个错误呢?参见如下:
Kai Ruottu wrote:

vinod wrote:

When I using gcc to compile my code.c to code , I got the error msg as
follows :
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o: In function
_start':
(.text+0x18): undefined reference to
main’
collect2: ld returned 1 exit status

What can I do now, any suggestion?
The solution for this depends on what you want to do:

1) if you want to build an executable from your source code file code.c,
you must obviously add a main function which then calls on of your
currently used main functions (whatever name that is) - already suggested

2) if main is defined in another source file - which I guess is true in
your case - you need to compile the file code.c only (by using the -c
compiler flag) and then link all object files to your executable.

As an example for the second solution. Assume you have two source files
main.c (defines the main function) and code.c. To build an executable,
you either use

gcc code.c main.c -o <exec_name>

or, if you want to compile in single steps

gcc code.c -c (produces code.o)
gcc main.c -c (produces main.o)
gcc code.o main.o -c <exec_name>

For the last step, you could theoretically also use ld to link the
executable, but I would not recommend this because you need to specify a
bunch of linker flags which are used by gcc any.

也就是说我没有搞清楚-c 和 -o的含义
-c: compile 编译的意思
-o: link 链接的意思。
而如果要链接的话,必须其中要有一个main函数。由于1.cpp 2.cpp中并没有main函数,所以会报错。
如果这样:

xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ g++ -o extern_test 2.cpp 1.cpp 3.cpp
xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ ls
1.cpp  2.cpp  3.cpp  extern_test  makefile
xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ ./extern_test
3
3
xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ 

就ok了,如果换个位置呢?看看编译器会不会介意顺序?

xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ g++ -o extern_test 3.cpp 1.cpp 2.cpp
xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ ./extern_test 
3
3
xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ 

真棒,不介意顺序。
那如果在3.cpp中去掉void print()声明呢?

#include <iostream>
//void print();
int main()
{
        extern int a;
        print();
        std::cout << std::endl;
        std::cout << a << std::endl;
}

编译结果是:

xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$ g++ -o extern_test 1.cpp 2.cpp 3.cpp
3.cpp: In function ‘int main()’:
3.cpp:6:8: error: ‘print’ was not declared in this scope
  print();
        ^
xiahuixia@xiahuixia-Inspiron-3437:~/c++/1$

错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值