OpenWrt C/C++ Development with Eclipse(Eclipse下的Openwrt开发)

来源:互联网 发布:linux dd 备份系统 编辑:程序博客网 时间:2024/05/29 17:16

OpenWrt C/C++ Development with Eclipse

 第一次翻译文章,肯定有翻译的不对的地方,还请各位不吝赐教。

原文:http://downloads.openwrt.org/docs/eclipse.pdf

1.Copyright

This work is licensed under the CreativeCommons Attribution-ShareAlike 3.0 Unported License.

To view a copy of this license, visithttp://creativecommons.org/licenses/by-sa/3.0/ or send a letter to CreativeCommons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

2.Purpose of document 目标

With Eclipse your are able to developsoftware for OpenWrt target devices in a very comfortable manner. Eclipseprovides a complete development suite.

Eclipse提供了一个完整的开发套件。使用Eclipse能够以一种非常舒适的方式进行OpenWrt目标设备的软件开发。

This document explains how to use EclipseC/C++ IDE 3.7 (Indigo) with OpenWrt's cross toolchain, how to setup remotetarget device source level debugging and remote access via eclipse. It is shownhow to write, compile and debug programs for OpenWrt target devices.

本文档介绍了如何通过EclipseC / C ++ IDE3.7(Indigo)使用OpenWrt的交叉工具链,如何设置远程目标设备的源代码级调试和远程访问。展示了如何编写,编译和调试OpenWrt目标设备程序。

3.OpenWrt Prerequisites

Install OpenWrt Buildroot then type

安装OpenWrtBuildroot然后键入:

tieto@vubuntu:~/openwrt/trunk$ make menuconfig

and check

选择

• enable (*) Build the OpenWrt SDK

• enable (*) “Build the OpenWrt based Toolchain”

• enable (*) “Advanced configuration options (fordevelopers)->Toolchain Options

→ “gdb”

→ “Build/install c++ compiler and libstdc++” (if C++ is required)

If not already done execute

如果尚未执行,则

make toolchain/install

4.Target Prerequisites目标系统必备组件

The following packages are required on yourtarget device:

在目标设备上,需要安装以下包:

• DropBear or OpenSSH installed & connections can be established

安装DropBear或OpenSSH以便建立连接

• libstdcpp (optional for C++)

• openssh-sftp-server

• gdbserver

5.Eclipse Prerequisites  Eclipse系统必备组件

Download your desired Eclipse IDE for C/C++Developers, extract archive, execute eclipse and enter workbench.

下载EclipseC/C++版,提取文件,执行eclipse并进入工作台。

We have to install additional eclipsepackages: enter Menu → Help → Install newSoftware .

我们必须安装额外的Eclipse包:

enter Menu → Help → Install newSoftware .

and select in section “Mobile and Device Development” packages“C/C++ GCC Cross Compiler Support” and “Remote System Explorer End-UserRuntime”.

选择部分“Mobileand Device Development”、“C/C++ GCC Cross Compiler Support”和“Remote System Explorer End-User Runtime”。


Finish and restart eclipse.

完成并重新启动Eclipse。

Create a new project: Menu → File → New C++ Project ( resp. Cproject).

创建新工程:Menu → File → New C++ Project ( resp. Cproject).

The next settings depends on your targetdevice and where your buildroot has been installed. We have to evaluate yourspecific target settings:

安装buildroot后,接下来的设置取决于目标设备。我们需要评估特定目标设备的设定:

6.1 Tool command prefix (target specific)  工具命令前缀(特定目标)

tieto@vubuntu:~/openwrt/trunk$ find ./staging_dir -path "./staging_dir/toolchain*" -name *openwrt-linux

On my build system I get as result

在我的编译系统,得到的结果如下:

./staging_dir/toolchain-mips_r2_gcc-4.5-linaro_uClibc-0.9.32/mips-openwrt-linux

./staging_dir/toolchain-i386_gcc-4.5-linaro_uClibc-0.9.32/i486-openwrt-linux

Hence for “Tool command prefix” we have toenter “mips-openwrt-linux-”for ar7xxx based resp. “i486-openwrt-linux-” for generic x86 / e.g. vmware target device.

 “Tool command prefix”的设置,对于ar7xxx目标设备,我们要输入“mips-openwrt-linux-”,对于类似于虚拟机目标设备的x86环境 “i486-openwrt-linux-”。

6.2 Tool command path (target specific) 工具命令路径(特定目标)

We can reuse the above finding results toget the required full path:

我们可以使用上面获得的查找结果,作为完整路径:

For above example ar7xxx target:

针对上面ar7xxx目标设备:

[YOUR_BR_HOME]/staging_dir/toolchain-mips_r2_gcc-4.5-linaro_uClibc-0.9.32

resp. for my generic x86 target:

针对通用的x86目标设备:

[YOUR_BR_HOME]/staging_dir/toolchain-i386_gcc-4.5-linaro_uClibc-0.9.32

Don't forget to adapt these settings toYOUR specific build environment, COPY + PASTE from here may not work !

注意:切记根据具体的构建环境进行设置,切勿直接复制文章中的设置。

Finally enter your specific setting andpress Finish button.

完成特定设置输入,并点击完成按钮。

6.3 Cross Compile Hello World 交叉编译Hello World

Add src folder and source file

添加src文件夹和源文件

Menu → File → New SourceFolder (src)

Menu → File → New SourceFile (src/HelloOpenwrt.cpp)

//================================================

// Name        : HelloOpenWrt.cpp

// Author      : irimi

// Version     :

// Copyright   : Your copyrightnotice

// Description : Hello World in C++, Ansi-style

//================================================

#include <iostream>

using namespace std;

int main() {

int i = 0;

for (i=0; i<10; i++)

{

cout << "Hello OpenWrt" << endl;

}

return 0;

}

If all was configured correctly you shouldbe able to call Menu->Project-Build all without errors.

调用Menu->Project->Buildall进行编译,如果配置正确的话不会产生错误。

But you can't excute the created bin fileon your build system, remember your binary is cross compiled☺.

我们无法在当前系统(PC)下运行刚刚创建的二进制文件,请牢记,生成的文件是由交叉编译工具链为目标设备生成的。

编译输出如下:

**** Build of configuration Debug for project HelloOpenWrt ****

make all

Building file: ../src/HelloOpenWrt.cpp

Invoking: Cross G++ Compiler

i486-openwrt-linux-g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP

-MF"src/HelloOpenWrt.d" -MT"src/HelloOpenWrt.d" -o"src/HelloOpenWrt.o"

"../src/HelloOpenWrt.cpp"

Finished building: ../src/HelloOpenWrt.cpp

Building target: HelloOpenWrt

Invoking: Cross G++ Linker

i486-openwrt-linux-g++  -o"HelloOpenWrt" ./src/HelloOpenWrt.o  

Finished building target: HelloOpenWrt

**** Build Finished ****

Your Eclipse IDE will look like this:

在Eclipse环境下看起来类似于这样:

7.Remote Target Setup 远程目标设置

If you program was successfully compiled,you have to install it on your target device to execute it.

程序编译成功后,必须安装到目标设备上去运行。

At this point Eclipse will help with a nicefeature: Menu → Window → Show View → Remote

Eclipse中有一个很好的功能:Menu → Window → Show View → Remote

System and we create a new connection:

创建一个新的连接在两个系统(PC和目标设备)之间:

Now select Linux

选择Linux


Now enter the target device's IP addressresp. hostname and in next screen select .ssh files

输入目标设备IP地址,主机名,并在下一个页面中选择 .ssh files。


Now select processes.shell.linux,

选择processes.shell.linux


and select ssh.shells

选择ssh.shells


and finally Finish !

完成了!

7.1 Browse Your Target Device 浏览目标设备

After entering user name + password you arenow able to put files on/from your target devices via drag n' drop, you can even control the target's processes.

输入用户名和密码,然后就可以向目标设备中拖拽文件,甚至还能控制目标设备上的进程。


Or you can enter a ssh terminal in eclipseor copy/execute HelloOpenWrt bin file on your target device.

另一种方法,可以在eclipse中选择ssh终端,使用这个终端上传并在目标设备中运行HelloOpenWrt二进制文件。

8.Remote gdb Debugger Setup远程gdb调试设置

For target device remote debugging at firstwe have to define a debug configuration Left click on “bug”-button to enter“Debug Configurations”

为了在目标设备中实现远程调试,我们需要对调试设置进行配置,点击bug按钮选择“DebugConfigurations”。


and we create a new C/C++ RemoteApplication Debug Configuration

新建一个C/C++Remote Application Debug Configuration


• In “Main” at C/C++ App adapt local file path to your application

在Main选项卡中的C/C++ Application中输入本地文件路径

• Change at “Connection”  toyour already defined target device remote connection (see chapter Remote TargetSetup).

在Connection中选在前面设置的远程连接(可参考第二章远程目标设置)

• don't forget to define the correct “Remote Absolute File Path forC/C++ Application”

别忘了定义正确的远程文件的绝对路径。


Now click on “Debugger” settings to definethe correct host gdb file.

现在就可以点击Debugger设置主机的gdb文件了。

8.1 Target specific host gdb 目标特定主机的gdb

As host gdb we can't use the /usr/bin/gdb provided e.g. by Ubuntu, we must use the gdb which has been built by ourtoolchain.As well as the tool command prefix, the location depends on your specific target settings and we evaluate it again.

关于目标设备的gdb,不能使用/usr/bin/gdb(Ubuntu中提供的那样),必须使用我们通过交叉编译工具链创建的gdb。除了gdb以外, tool command prefix的地址也依赖于特定的目标系统设置和再次评估。

It is located somewhere in ./build_dir:

dbg位于某个./build_dir中:

tieto@vubuntu:~/openwrt/trunk$ find ./build_dir -executable -type f -name gdb |grep toolchain

I got this result:

我的搜索结果是这样的:

./build_dir/toolchain-i386_gcc-4.5-linaro_uClibc-0.9.32/gdb-linaro-7.2-2011.03-0/gdb/gdb

We have to enter the absolute file path at“GDB debugger”

我们需要在GDBdebugger中输入文件的绝对路径。

Remember these settings depend on YOURspecific build environment, COPY + PASTE from here may not work !!

注意:请牢记,此处的设置取决于具体的构建环境,切勿直接复制文章中的配置。


Other changes are not required. Now you maypress “Debug button” of settings window.

其他设置并不是必须的,现在就可以按设置窗口的的Debug button。

9.Remote Debugging Example 远程调试示例

When you launch HelloOpenWrt Debugavailable at ”Bug-Button” the Debug View of eclipse will be opened.

当启动HelloOpenWrt的调试后,将打开Eclipse的Debug窗口。


The above warning can be ignored since youdon't have enabled “Advanced configuration options (for developers)->BuildOptions->Debugging” in OpenWrt settings and rebuild all with debugging infos.

如果已经在Openwrt中设置了“Advanced configuration options (fordevelopers)->Build Options->Debugging”并且根据调试信息重新编译了程序,则上述警告可以忽略。

Congratulations, now you have a completeOpenWrt Development Suite !

恭喜,至此你拥有了一个完整的OpenWrt开发套件!



经过几天艰辛的尝试,终于成功了!截图如下: