为什么我不能在 mac 上使用 g++ 运行这个简单的 c++ 代码
Why I cannot run this simple c++ code by using g++ on mac?
我现在正在研究C++,我正在尝试学习如何使用头文件。但是,当我运行程序时总是会出错。
/* File add.h */
#ifndef ADD_H
#define ADD_H
int add(int, int);
#endif
/* ADD_H */
/* File add.cpp */
#include "add.h"
int add(int a, int b)
{
return a + b;
}
/* File triple.cpp */
#include<iostream>
#include "add.h"
using namespace std;
int triple(int);
int triple(int x)
{
return add(x, add(x, x));
}
int main()
{
int i=0;
int j;
while (i<=5)
{ j=triple(i);
cout<<j<<endl;
//cout<<triple(j)<<endl;
i++;
}
return 0;
}
这是我使用的 3 个文件。当我运行时:g++ 三重.cpp在Mac上,错误如下:
Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
triple(int) in triple-e0558f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
谁能给我一些关于这个错误的提示。多谢!顺便说一下,gcc版本信息如下:
gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
两种编译方式:
a. 逐个编译
g++ -c add.cpp -o add.o
g++ -c triple.cpp -o triple.o
g++ add.o triple.o -o triple
b. 一次编译所有内容
g++ add.cpp triple.cpp -o triple
相关文章:
- 无法运行简单的N-API hello-world示例
- 无法获得最简单的JNI足以在Windows 10上运行
- 运行简单的.oct文件时Octave崩溃,我的代码出了什么问题?
- 为什么这个简单的 C++ SIMD 基准测试在使用 SIMD 指令时运行速度较慢?
- 将简单的 exe 运行到运行 Microsoft/WindowsServerCore 的 docker 容器中
- 如何在void*指针下检查运行时类型(允许简单类型,没有基类)?
- 如何直接在安卓设备上作为可执行文件运行简单的 c++ 程序?
- 运行简单 c++ 程序时出现权限被拒绝错误
- 运行简单的C 功能
- 无法运行简单的 std::async 和 std::future 测试程序。错误:"has initializer but incomplete type."发生了什么?
- 无法在Linux Minut中运行简单的OpenGL程序
- 无法使用 QProcess 运行简单的控制台程序
- 尝试在64位数据类型上运行简单模板内核时,cudaErrorLaunchFailure
- 可以't运行简单的Visual Studio 2013项目
- 在 Linux 上使用 GCC 4.6 和 4.7 运行简单 std::thread 代码时的核心转储
- 无法在 CLion 中运行简单的程序
- DirectX 11:运行简单DirectX应用程序时运行时崩溃
- Visual Studio 2010 在尝试运行简单的"Hello World"代码时崩溃?
- 无法运行简单的 mpi 程序
- 使用Caffe运行简单的c++程序