首页 > 解决方案 > C++“系统(命令)”在 NetBeans / Windows 上不起作用

问题描述

我正在使用 Windows 上的 CodeBlocks 开发 C++ 项目,但后来决定切换到 NetBeans IDE 8.2。

在我的项目中,我正在调用另一个带有一些传递参数的可执行文件(我使用合适的参数运行另一个 .exe,然后我将它的输出用于我的主项目),它曾经在 CodeBlocks 上工作,但在 NetBeans 上不工作.

代码如下:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include "My_Constants.h"
#include "Data.h"
#include "Parameters.h"
#include "Pattern.h"
#include "Squish.h"
#include "Deserializer.h"
#include "Automatic_Run.h"

using namespace std;

int main()
{

    Parameters parameters;
    parameters.mode = SQUISH;
    Automatic_Run runner;
    string inputname;

    //--------------------------------User Input-------------------------------------
    cout << "enter input file name \n";
    getline(cin, inputname);
    parameters.inputFileName.assign(inputname);
    cout<<"=============================================================================\n";

    //--------------------------------Running SQUISH/first call--------------------------
    cout<<"Running SQUISH - first call\n";
    char command[1000]="";
    string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
    strcat(command,"C:\\Users\\Administrator\\Documents\\CodeBlocksProjects\\TestSQUISH\\bin\\Debug\\TestSQUISH.exe ");
    strcat(command,passedParameters.c_str());
    int result = system(command);


 // the rest of the code(not relevant to the problem)

    return 0;
}

在 CodeBlocks 上,它曾经完美地工作并在我的主项目(我从中调用 TestSQUISH 的那个)路径中将输出作为文件提供给我。但是,现在在 NetBeans 上,我收到以下错误:

sh: C:UsersAdministratorDocumentsCodeBlocksProjectsTestSQUISHbinDebugTestSQUISH.exe: 找不到命令

我检查了 NetBeans 的终端以了解正在发生的事情(假设它可能相关),我注意到我必须先更改路径,然后使用以下命令运行可执行文件:

./ TestSQUISH.exe (+参数)

但是,这也不适用于我的项目。

谁能建议一个解决方案或告诉我如何让 NetBeans 像 CodeBlocks 过去那样在 Windows 终端上运行命令?

标签: c++netbeanssystemcodeblocks

解决方案


感谢@Yksisarvinen 的评论,我能够解决问题。

注意到 NetBeans 使用的是 shell 而不是 Windows 样式的命令,并且在使用 NetBeans 自己的终端真正清楚地了解它如何转换路径之后,我能够使用以下命令成功运行代码:

char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command, "/cygdrive/c/Users/Administrator/Documents/CodeBlocksProjects/TestSQUISH/bin/Debug/TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);

Netbeans 终端添加cygdrive到路径的开头,并c使用C:.

如果可执行文件与您自己的项目位于同一目录中,那么这就足够了:

char command[1000]="";
string passedParameters = " -i "+parameters.inputFileName +" -f "+ "t";
strcat(command ,"./TestSQUISH.exe ");
strcat(command,passedParameters.c_str());
int result = system(command);

推荐阅读