'cmath' file not found

本文详细介绍了如何在C++项目中解决Box2D库中的b2Vec2类型与Cocos2D库中数学类型的重复利用问题,通过将main.m改为main.mm文件,成功解决了cmath、iostream等相关预编译指令无法正常引入的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

box2d 的 b2Vec2 类型和 cocos2d 的 CGPoint,CGSize, CGRect,我想拿出来自己用,

于是新建了一个 Terminal 工程。

因为 CGPoint 和 苹果自带的 CoreGraphics里面自带的~

而 box2d 里面的 b2Vec2 类型我也经常用到,

包含 b2Vec2 类型定义的头文件 b2Math.h 里面同时也包含了不少 有用的,线程的方法,

因此我很想来重复利用这些现成的东西。

但是遇到了一些问题:

‘cmatch’ file no found, 'iostream' not recognized等等,

包含在 b2Math.h 的

#include <cmath>

#include <cfloat>

#include <cstddef>

#include <cassert>

#include <limits>

等预编译指令,竟然每一个都有问题,都无法正常的引入!

后来找到了一篇帖子:

C++ #include help?

Quote:
Originally Posted by BlackBlade View Post
cmath related errors are simply this, "cmath" file not found, and iostream is not recognized, i then went through the whole list of include functions(not sure what to call them) and the closest thing on there is IOsurface or IOsurface.h
Did you create a C++ project and not Objective-C or Cocoa? I you pick a Cocoa application you will get this error.



Quote:
Originally Posted by subsonix View Post
Did you create a C++ project and not Objective-C or Cocoa? I you pick a Cocoa application you will get this error.
i am an idiot, i figured it out, i never made a file in the project i simply used main.m
just tried making a c++ file and it worked

thanks a lot, now i won't fail! thanks again


根据帖子里面所提到的相关信息,在我将 main.m 改成 main.mm 文件以后,编译成功通过~


gdb 调试启动失败 path not found launch.json 配置调试路径 c:\Users\General\Desktop\一个文件夹\C\练习{ "configurations": [ { "name": "Win64", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "C:/MinGW/bin/g++.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 }{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }{ "files.associations": { "*.py": "python", "iostream": "cpp", "*.tcc": "cpp", "string": "cpp", "unordered_map": "cpp", "vector": "cpp", "ostream": "cpp", "new": "cpp", "typeinfo": "cpp", "deque": "cpp", "initializer_list": "cpp", "iosfwd": "cpp", "fstream": "cpp", "sstream": "cpp", "map": "c", "stdio.h": "c", "algorithm": "cpp", "atomic": "cpp", "bit": "cpp", "cctype": "cpp", "clocale": "cpp", "cmath": "cpp", "compare": "cpp", "concepts": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", "cstdlib": "cpp", "cstring": "cpp", "ctime": "cpp", "cwchar": "cpp", "exception": "cpp", "ios": "cpp", "istream": "cpp", "iterator": "cpp", "limits": "cpp", "memory": "cpp", "random": "cpp", "set": "cpp", "stack": "cpp", "stdexcept": "cpp", "streambuf": "cpp", "system_error": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "xfacet": "cpp", "xiosbase": "cpp", "xlocale": "cpp", "xlocinfo": "cpp", "xlocnum": "cpp", "xmemory": "cpp", "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", "xtree": "cpp", "xutility": "cpp", "stdlib.h": "c", "string.h": "c" }, "editor.suggest.snippetsPreventQuickSuggestions": false, "aiXcoder.showTrayIcon": true }{ "version": "2.0.0", "tasks": [ { "label": "g++", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe" ], "problemMatcher": { "owner": "cpp", "fileLocation": [ "relative", "${workspaceRoot}" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "group": "build" }, { "type": "cppbuild", "label": "C/C++: g++.exe 生成活动文件", "command": "C:/MinGW/bin/g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "C:/MinGW/bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ] }
06-21
#include <iostream> #include <cmath> #include <chrono> #include <thread> #include <vector> #include <gmpxx.h> #include <fstream> #include <sstream> #include <iomanip> #include <mutex> #include <atomic> #include <sys/sysinfo.h> #include <sys/resource.h> #include <fstream> #include <unistd.h> #include <nvml.h> // NVIDIA GPU监控 std::mutex output_mutex; std::atomic<bool> stop_monitor(false); mpf_class pi_value(0, 0); // 高精度π值 std::atomic<int> iteration_count(0); // 获取CPU使用率 double get_cpu_usage() { static uint64_t last_total = 0, last_idle = 0; std::ifstream stat_file("/proc/stat"); std::string line; std::getline(stat_file, line); std::istringstream iss(line); std::string cpu; uint64_t user, nice, system, idle, iowait, irq, softirq, steal; iss >> cpu >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal; uint64_t total = user + nice + system + irq + softirq + steal; uint64_t idle_time = idle + iowait; double usage = 0.0; if (last_total != 0) { uint64_t total_diff = total - last_total; uint64_t idle_diff = idle_time - last_idle; usage = 100.0 * (1.0 - static_cast<double>(idle_diff) / total_diff); } last_total = total; last_idle = idle_time; return usage; } // 获取内存使用率 double get_memory_usage() { struct sysinfo mem_info; sysinfo(&mem_info); double total = mem_info.totalram; double free = mem_info.freeram; return 100.0 * (1.0 - free / total); } // 获取GPU使用率 (NVIDIA) double get_gpu_usage() { nvmlReturn_t result; nvmlDevice_t device; unsigned int utilization; result = nvmlInit(); if (result != NVML_SUCCESS) return -1.0; result = nvmlDeviceGetHandleByIndex(0, &device); if (result != NVML_SUCCESS) { nvmlShutdown(); return -1.0; } result = nvmlDeviceGetUtilizationRates(device, &utilization); if (result != NVML_SUCCESS) { nvmlShutdown(); return -1.0; } nvmlShutdown(); return static_cast<double>(utilization.gpu); } // 资源监控线程函数 void monitor_resources() { while (!stop_monitor) { double cpu = get_cpu_usage(); double mem = get_memory_usage(); double gpu = get_gpu_usage(); { std::lock_guard<std::mutex> lock(output_mutex); std::cout << "\033[2K"; // 清除当前行 std::cout << "\r[资源监控] CPU: " << std::fixed << std::setprecision(1) << cpu << "% | " << "内存: " << mem << "% | " << "GPU: " << (gpu >= 0 ? std::to_string(static_cast<int>(gpu)) + "%" : "N/A"); std::cout.flush(); } std::this_thread::sleep_for(std::chrono::seconds(1)); } } // Chudnovsky算法计算π mpf_class chudnovsky_pi(unsigned int precision, unsigned int iterations) { mpf_set_default_prec(precision); mpf_class pi(0, precision); mpf_class sum(0, precision); mpf_class k1(13591409, precision); mpf_class k2(545140134, precision); mpf_class k3(-262537412640768000, precision); mpf_class k4(426880, precision); mpf_class k5(10005, precision); mpf_class sqrt_c(0, precision); k5 = sqrt(k5); sqrt_c = k4 * k5; mpz_class a(1); mpz_class b(1); mpz_class a_prime(1); mpz_class b_prime(1); for (unsigned int k = 0; k < iterations; k++) { mpf_class numerator(0, precision); mpf_class denominator(0, precision); if (k == 0) { a = 1; b = 1; } else { // 使用二进制分裂法优化计算 a_prime = (6*k-5)*(2*k-1)*(6*k-1); b_prime = k*k*k * k3 / 24; a *= a_prime; b *= b_prime; } numerator = k1 + k2 * k; denominator = a * b; sum += numerator / denominator; iteration_count = k + 1; // 每100次迭代显示进度 if (k % 100 == 0) { pi = sqrt_c / sum; std::lock_guard<std::mutex> lock(output_mutex); std::cout << "\n[计算进度] 迭代: " << k << "/" << iterations << " | 当前π值: " << std::setprecision(15) << pi << std::endl; } } pi = sqrt_c / sum; return pi; } int main() { const unsigned int precision = 100000; // 二进制精度位 const unsigned int iterations = 1000; // 迭代次数 std::cout << "开始计算π值 (Chudnovsky算法)..." << std::endl; std::cout << "精度: " << precision << "位 | 最大迭代: " << iterations << std::endl; // 启动资源监控线程 std::thread monitor_thread(monitor_resources); // 记录开始时间 auto start_time = std::chrono::high_resolution_clock::now(); // 计算π值 pi_value = chudnovsky_pi(precision, iterations); // 记录结束时间 auto end_time = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); // 停止监控线程 stop_monitor = true; monitor_thread.join(); // 输出最终结果 std::cout << "\n\n计算完成!" << std::endl; std::cout << "耗时: " << duration.count() / 1000.0 << " 秒" << std::endl; std::cout << "最终π值(前50位): " << std::setprecision(50) << pi_value << std::endl; return 0; } 此代码报错,修复错误 报错信息:default.cpp:6:10: fatal error: 'gmpxx.h' file not found #include <gmpxx.h> ^~~~~~~~~ 1 error generated. 输出完整代码
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值