【问题标题】:C++ "system(command)" not working on NetBeans / WindowsC++“系统(命令)”在 NetBeans / Windows 上不起作用
【发布时间】:2019-12-04 10:53:34
【问题描述】:

我正在开发一个在 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++。请提供您正在编译和执行的确切代码。
  • 我猜你需要 4x\ 而不是 2x\
  • 谢谢,我试过了,它解决了“/”问题,但是找不到命令的主要问题仍然存在:sh: C:\Users\Administrator\Documents\CodeBlocksProjects\TestSQUISH\bin \Debug\TestSQUISH.exe:找不到命令@JVApen
  • 目录的完整路径是否存在?可执行文件是否存在?
  • 从错误消息看来,NetBeans 使用了sh shell(不知何故)。你确定它理解以C:开头的Windows风格的路径吗?

标签: c++ netbeans system codeblocks


【解决方案1】:

进入项目设置,将要执行的项目路径设置为其他应用所在的文件夹。

设置系统路径以包含该文件夹。

【讨论】:

    【解决方案2】:

    感谢@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);
    

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 2017-01-23
      相关资源
      最近更新 更多