c ++在linux中编译并运行[复制]

时间:2022-07-15 03:15:19

Possible Duplicate:
Undefined reference to

可能重复:未定义的引用

In my directory I have:

在我的目录中,我有:

main.cpp
tree.cpp
tree.h

I have included tree.h in main.cpp

我在main.cpp中包含了tree.h.

#include "tree.h"

then in my main function I write

然后在我写的主要功能中

tree* t=new tree()

For compiling I will do

为了编译我会做

g++ main.cpp

but I have the error

但我有错误

undefined reference to `tree::tree()'

what's the problem?

有什么问题?

3 个解决方案

#1


7  

You need to compile and link Tree source code as well:

您还需要编译和链接Tree源代码:

$ g++ -c -o tree.o tree.cpp
$ g++ -o test main.cpp tree.o

run your application:

运行你的申请:

$ ./test

#2


2  

You might want to create yourself a make file for that. A make file would automate the compilation of multiple files programs.

你可能想为自己创建一个make文件。 make文件将自动编译多个文件程序。

For example, you could create a file "makefile" containing the lines suggested by billz.

例如,您可以创建一个包含billz建议的行的文件“makefile”。

all:
    g++ -c -o tree.o tree.cpp
    g++ -o test main.cpp tree.o

Then, running make from the terminal in the makefile's folder would execute the all section.

然后,从makefile文件夹中的终端运行make将执行all部分。

For more information on makefiles, see http://www.cs.bu.edu/teaching/cpp/writing-makefiles/

有关makefile的更多信息,请参阅http://www.cs.bu.edu/teaching/cpp/writing-makefiles/

I haven't tested the above code, it's possible that some tweaking would be necessary.

我没有测试过上面的代码,可能需要进行一些调整。

#3


0  

Put the header guard, something like this

把头卫,像这样的东西

#ifndef NAME_H
#define NAME_H

//your codes

#endif

#1


7  

You need to compile and link Tree source code as well:

您还需要编译和链接Tree源代码:

$ g++ -c -o tree.o tree.cpp
$ g++ -o test main.cpp tree.o

run your application:

运行你的申请:

$ ./test

#2


2  

You might want to create yourself a make file for that. A make file would automate the compilation of multiple files programs.

你可能想为自己创建一个make文件。 make文件将自动编译多个文件程序。

For example, you could create a file "makefile" containing the lines suggested by billz.

例如,您可以创建一个包含billz建议的行的文件“makefile”。

all:
    g++ -c -o tree.o tree.cpp
    g++ -o test main.cpp tree.o

Then, running make from the terminal in the makefile's folder would execute the all section.

然后,从makefile文件夹中的终端运行make将执行all部分。

For more information on makefiles, see http://www.cs.bu.edu/teaching/cpp/writing-makefiles/

有关makefile的更多信息,请参阅http://www.cs.bu.edu/teaching/cpp/writing-makefiles/

I haven't tested the above code, it's possible that some tweaking would be necessary.

我没有测试过上面的代码,可能需要进行一些调整。

#3


0  

Put the header guard, something like this

把头卫,像这样的东西

#ifndef NAME_H
#define NAME_H

//your codes

#endif