You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Windows 10 64位环境下编译Android平台.cpp文件遇错求助

解决Android NDK编译C++代码找不到iostream的问题

Hey there, let's get your C++ code compiled for Android properly! The error you're seeing (fatal error: iostream: No such file or directory) has clear fixes, let's walk through them step by step:

1. 核心问题:用错了编译器

你当前执行的命令用了x86_64-linux-android-gcc.exe——这是C语言专用编译器,它默认不会识别C标准库的头文件(比如iostream)。要编译C代码,必须使用对应的C++编译器:x86_64-linux-android-g++.exe。

2. 修正编译命令

把你的命令替换成下面的正确版本(注意去掉输出文件名末尾多余的点):

x86_64-linux-android-g++.exe main.cpp -o main

额外注意事项

  • 确保Android NDK工具链的bin目录已添加到系统PATH环境变量,这样CMD能直接找到编译工具。如果没添加,就用工具的完整路径调用,比如:
    D:\android-ndk-r25b\toolchains\llvm\prebuilt\windows-x86_64\bin\x86_64-linux-android-g++.exe main.cpp -o main
    
  • 如果目标Android设备是其他架构,要替换对应编译器:
    • arm64-v8a架构:aarch64-linux-android-g++.exe
    • armeabi-v7a架构:armv7a-linux-androideabi-g++.exe

顺便修正你的代码小问题

你的代码里的宏定义末尾多了个分号,会导致语法错误,建议修正(同时加上main函数的返回值,符合C++标准):

#include <iostream>
#include <cmath>
using namespace std;

#define greater(x,y) (x<y?y:x)

int main(){
    int a,b;
    cout<<"Enter two numbers "<<endl;
    cin>>a>>b;
    cout<< "The greater number is "<<greater(a,b);
    return 0;
}

做完这些修改后,重新执行编译命令就能成功生成Android平台的可执行文件啦!

内容的提问来源于stack exchange,提问作者Yamin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:42:44