gpt4 book ai didi

c++ - 无法使用 C++ stdlib 系统调用运行 make

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:35 28 4
gpt4 key购买 nike

我在 C++ 中得到了以下代码

  if (should_run_make) {
std::string make = "make -C ";
make.append(outdir);
std::cout << "Make cmd is " << make << std::endl;
system(make.c_str());
}

报告如下:

Make cmd is make -C /home/hamiltont/temp/ make: Entering directory /home/hamiltont/temp' make: *** No targets. Stop.
make: Leaving directory
/home/hamiltont/temp'

但是,以多种方式手动执行此操作效果很好,例如

[hamiltont@4 generator]$ make -C /home/hamiltont/temp/
make: Entering directory `/home/hamiltont/temp'
g++ -O3 -I/usr/include/openmpi-x86_64 -L/usr/local/lib -L/usr/lib64/openmpi/lib -lmpi -lmpi_cxx -lboost_serialization -lboost_mpi stg_impl.cpp -o impl
make: Leaving directory `/home/hamiltont/temp'

[hamiltont@4 generator]$ cd /home/hamiltont/temp/
[hamiltont@4 temp]$ make
g++ -O3 -I/usr/include/openmpi-x86_64 -L/usr/local/lib -L/usr/lib64/openmpi/lib -lmpi -lmpi_cxx -lboost_serialization -lboost_mpi stg_impl.cpp -o impl

最佳答案

您是从 C 程序中生成联编文件吗?这是我能想到的唯一会导致特定错误消息的原因。

make: *** No targets. Stop. 

Reproducing the error

Here's how I could generate that message:

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("Makefile", "w");
fputs("all:\n\techo Done.\n", fp);
system("make");
fclose(fp);
return 0;
}

这可以预见地打印:

make: *** No targets.  Stop.

I say predictably because Makefile will be empty! This is because IO is buffered...

Fixed version

So, I close the file before calling system(), which flushes the buffer (fflush() would also do the trick):

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("Makefile", "w");
fputs("all:\n\techo Done.\n", fp);
fclose(fp);
system("make");
return 0;
}

输出:

echo Done.Done.

为了清楚起见,我使用了 C 的 IO 函数,但同样的规则适用于 <iostream> .

关于c++ - 无法使用 C++ stdlib 系统调用运行 make,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15214327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com