c++在linux下编译并运行
c++ compile and run in linux
可能重复:
未定义的参考
在我的目录中,我有:
main.cpp
tree.cpp
tree.h
我已将tree.h包含在main.cpp 中
#include "tree.h"
然后在我的主要功能中,我写
tree* t=new tree()
为了编译,我会做
g++ main.cpp
但是我有错误
undefined reference to `tree::tree()'
怎么了?
您还需要编译并链接Tree源代码:
$ g++ -c -o tree.o tree.cpp
$ g++ -o test main.cpp tree.o
运行您的应用程序:
$ ./test
您可能需要为自己创建一个make文件。make文件将自动编译多个文件程序。
例如,您可以创建一个包含billz建议的行的文件"makefile"。
all:
g++ -c -o tree.o tree.cpp
g++ -o test main.cpp tree.o
然后,从makefile文件夹中的终端运行make
将执行all
部分。
有关生成文件的详细信息,请参见http://www.cs.bu.edu/teaching/cpp/writing-makefiles/
我还没有测试过上面的代码,可能需要进行一些调整。
放置标题保护,类似于
#ifndef NAME_H
#define NAME_H
//your codes
#endif
相关文章:
- 如何在linux终端中同时编译和运行c++代码
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 尝试用java代码编译和运行c++代码
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- 编译并运行后,输出未出现在 Visual Studio 代码中
- 无法使用犰狳运行编译的 cpp 代码,因为 dyld:库未加载
- 运行编译的 a.out 后出现分段错误(核心转储)错误
- 如果使用 PHP 运行编译,则无法分配内存
- msys 和 tdm-gcc:配置错误 - 无法运行 C 编译的程序
- Eclipse CDT - 如何在外部控制台 (cmd.exe) 中运行编译.exe
- 无法在 OSX 上运行编译的 C++ 可执行文件,适用于 Debian
- Linphone编译-配置:错误:无法运行C编译的程序
- 错误0xc000007b:试图在没有VS的计算机上运行编译为Release x86的C++.exe文件
- 在windows上使用oud cmd运行g++编译的文件
- Eclipse CDT 无法在 Windows 中运行编译的可执行文件,并显示错误"terminated, exit value: <some number>"
- 运行编译的c++代码与Rcpp
- "Cannot execute binary file"在 Ubuntu for Windows 平台上使用 Eclipse 运行编译的 c++ 程序时
- 如何在未安装 CUDA 工具包的机器上运行编译的 CUDA 代码?
- visualstudio2010-尝试运行/编译我的c++程序