C++ 命令行参数 Eclipse CDT?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8390356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ command line arguments Eclipse CDT?
提问by user1028641
I am using an example program from this code http://sicktoolbox.sourceforge.net/> http://sourceforge.net/projects/sicktoolbox/files/. It's basically a distance scanner driver. The program I'm trying to run is in sicktoolbox-1.0.1/c++/examples/lms/lms_plot_values in case you wanted to see the code I'm talking about.
我正在使用此代码中的示例程序http://sicktoolbox.sourceforge.net/> http://sourceforge.net/projects/sicktoolbox/files/。它基本上是一个距离扫描仪驱动程序。我试图运行的程序在sicktoolbox-1.0.1/c++/examples/lms/lms_plot_values 中,以防您想查看我正在谈论的代码。
Anyways, the lms_plot_values project folder contains gnuplot_i.cc, gnuplot_i.hpp, main.cc, Makefile, Makefile.am, Makefile.in. So I put the first three files in my Eclipse Indigo CDT, compile (no compiler errors, everything's correctly linked in Eclipse already and all needed libraries are added), but this sample program is written to take in command line arguments. Here is as far as the code gets.
无论如何,lms_plot_values 项目文件夹包含 gnuplot_i.cc、gnuplot_i.hpp、main.cc、Makefile、Makefile.am、Makefile.in。所以我将前三个文件放在我的 Eclipse Indigo CDT 中,编译(没有编译器错误,Eclipse 中的所有内容都已正确链接,并且添加了所有需要的库),但编写此示例程序是为了接受命令行参数。就代码而言,这里是。
/*!
* \file main.cc
* \brief Illustrates how to acquire a measurements from the Sick
* LMS 2xx using the configured measuring mode.
*
* Note: This example should work for all Sick LMS 2xx models.
*
* Code by Jason C. Derenick and Thomas H. Miller.
* Contact derenick(at)lehigh(dot)edu
*
* The Sick LIDAR Matlab/C++ Toolbox
* Copyright (c) 2008, Jason C. Derenick and Thomas H. Miller
* All rights reserved.
*
* This software is released under a BSD Open-Source License.
* See http://sicktoolbox.sourceforge.net
*/
/* Implementation dependencies */
#include <stdlib.h>
#include <string>
#include <vector>
#include <signal.h>
#include <iostream>
#include <sicklms-1.0/SickLMS.hh>
#include "gnuplot_i.hpp"
using namespace std;
using namespace SickToolbox;
bool running = true;
void sigintHandler(int signal);
int main(int argc, char * argv[]) {
string device_str; // Device path of the Sick LMS 2xx
SickLMS::sick_lms_baud_t desired_baud = SickLMS::SICK_BAUD_38400;
/* Check for a device path. If it's not present, print a usage statement. */
if ((argc != 2 && argc != 3) || (argc == 2 && strcasecmp(argv[1],"--help") == 0)) {
cout << "Usage: lms_plot_values PATH [BAUD RATE]" << endl
<< "Ex: lms_plot_values /dev/ttyUSB0 9600" << endl;
return -1;
}
As it says, it throws an error and kills the program, saying it wants me to type "lms_plot_values /dev/ttyUSB0 9600" from the command line to run the program, but I can't do that, and I'm wanting to do everything in eclipse so I don't want to do that. I tried adding:
正如它所说,它抛出一个错误并杀死程序,说它希望我从命令行输入“lms_plot_values /dev/ttyUSB0 9600”来运行程序,但我不能这样做,我想在 Eclipse 中做所有事情,所以我不想那样做。我尝试添加:
argv[1] = "/dev/ttyUSB0";
argv[2] = "9600";
But that didn't work because of the argc checks. Do you know if it says to pass in "lms_plot_values /dev/ttyUSB0 9600", why it would be expecting or where it would be getting the argc values from? Or how I can make it think these parameters were passed in? I'm not very familiar with how C++ works like this, I've only used Java.
但由于 argc 检查,这不起作用。你知道它是否说要传入“lms_plot_values /dev/ttyUSB0 9600”,它为什么会期待或从哪里获取 argc 值?或者我怎么能让它认为这些参数是传入的?我不太熟悉 C++ 是如何工作的,我只使用过 Java。
Thanks for any help
谢谢你的帮助
回答by havexz
You can pass arguments in eclipse too. Once you build your project, try creating a run configuration and there you can pass the arguments. Here is the screen shot:
您也可以在 eclipse 中传递参数。构建项目后,尝试创建运行配置,然后您就可以传递参数了。这是屏幕截图: