C++命令行参数 Eclipse CDT?
我正在使用此代码中的示例程序 http://sicktoolbox.sourceforge.net/ > http://sourceforge.net/projects/sicktoolbox/files/ 。它基本上是一个距离扫描仪驱动程序。我尝试运行的程序位于icktoolbox-1.0.1/c++/examples/lms/lms_plot_values 中,以防您想查看我正在讨论的代码。
无论如何,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;
}
正如它所说,它抛出一个错误并终止程序,说它希望我从命令行输入“lms_plot_values /dev/ttyUSB0 9600”来运行程序,但我不能这样做,而且我想要在 eclipse 中做所有事情,所以我不想这样做。我尝试添加:
argv[1] = "/dev/ttyUSB0";
argv[2] = "9600";
但由于 argc 检查,这不起作用。你知道它是否说要传递“lms_plot_values /dev/ttyUSB0 9600”,为什么它会期望或者从哪里获取argc值?或者我如何让它认为这些参数已传入?我不太熟悉C++是如何工作的,我只使用过Java。
感谢您的帮助
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.
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.
/*!
* \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:
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.
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也可以在 eclipse 中传递参数。构建项目后,尝试创建运行配置,您可以在其中传递参数。这是屏幕截图:
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: