我正在尝试读取 C/C++ 中的“Makefile ”
它的工作原理是: system("make -C/Users/MyNAME/Desktop/Folder flash");
“make flash”是运行文件的命令,/Users/MyNAME/Desktop/Folder是
文件路径。这段代码可以工作,但是我如何在本地创建文件路径,以从同一个读取
调试文件夹(程序所在的位置)而不手动编写它?
最佳答案
您可能想要 int chdir(const char *path);
功能。示例:
#include <unistd.h>
/* ... */
chdir("/Users/MyNAME/Desktop/Folder");
system("make flash");
<unistd.h>
还有很多其他有用的功能,它应该存在于任何基于 POSIX 的系统上。 Check here为大名单。
编辑:如果路径不是恒定的,您可能希望将其作为命令行参数提供给程序:
#include <unistd.h>
#include <stdio.h>
/* ... */
int main(int argc, char* argv[])
{
if(argc < 2)
{
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
return 1;
}
chdir(argv[1]);
system("make flash");
/* ... */
return 0;
}
使用类似 ./foo /Users/MyNAME/Desktop/Folder
的行运行它.
关于c++ - C/C++ - 以编程方式查找 Makefile 文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8578086/