3. 使用Wind River Linux开发环境

任务目标

在VxWorks Workbench中配置Wind River Linux交叉编译环境,编译一个可在目标Linux设备上运行的简单C应用程序。本案例聚焦于工具链配置与编译验证闭环,不涉及内核或驱动开发。

环境准备

  • 已安装Wind River Linux开发环境(含交叉编译工具链)
  • Workbench 4.0及以上版本,已安装Linux开发插件
  • 目标平台:x86_64或ARM(本案例以x86_64为例)

实现步骤

1. 创建Linux应用工程

在Workbench中:File → New → Project → Wind River Linux → Linux User Application,工程类型选择Executable,工具链选择Wind River Linux x86_64(或对应目标架构)。

2. 编写应用代码

在工程源文件目录下创建hello_wrlinux.c,输入以下完整代码:

```c #include #include #include #include #define VERSION "1.0.0" static void print_usage(const char *prog) { printf("Usage: %s [options]\n", prog); printf("Options:\n"); printf(" -n Set greeting name (default: Wind River Linux)\n"); printf(" -v Show version\n"); printf(" -h Show this help\n"); } int main(int argc, char *argv[]) { const char *name = "Wind River Linux"; int opt; while ((opt = getopt(argc, argv, "n:vh")) != -1) { switch (opt) { case 'n': name = optarg; break; case 'v': printf("hello_wrlinux version %s\n", VERSION); return 0; case 'h': default: print_usage(argv[0]); return (opt == 'h') ? 0 : 1; } } printf("Hello, %s!\n", name); printf("Running on: "); fflush(stdout); FILE *fp = fopen("/proc/version", "r"); if (fp) { char buf[256]; if (fgets(buf, sizeof(buf), fp)) { printf("%s", buf); } fclose(fp); } else { printf("(unknown Linux kernel)\n"); } return 0; } ```

3. 配置交叉编译环境

右键工程 → Properties → C/C++ Build → Settings,确认以下关键配置:

  • Cross Compiler:命令前缀为x86_64-wrlinux-gnu-(ARM目标则为aarch64-wrlinux-gnu-)
  • Include Paths:添加${WIND_LINUX_SDK_ROOT}/sysroots/x86_64-wrlinux/sysroot/usr/include
  • Sysroot:设置为${WIND_LINUX_SDK_ROOT}/sysroots/x86_64-wrlinux/sysroot

若环境变量WIND_LINUX_SDK_ROOT未自动设置,需在Window → Preferences → Wind River → Linux中指定SDK安装路径。

4. 编译工程

点击工具栏的锤子图标(Build),或右键工程选择Build Project。编译输出应显示:

make all
x86_64-wrlinux-gnu-gcc -O2 -Wall -o hello_wrlinux hello_wrlinux.c

编译成功后,工程Debug/或Release/目录下生成可执行文件hello_wrlinux。

5. 验证方法

将生成的可执行文件拷贝到目标Linux设备(或本机Linux环境),执行以下命令验证:

./hello_wrlinux
./hello_wrlinux -n "VxWorks User"
./hello_wrlinux -v

通过条件:

  • 第一条命令输出以Hello, Wind River Linux!开头,并显示目标内核版本信息
  • 第二条命令输出Hello, VxWorks User!
  • 第三条命令输出hello_wrlinux version 1.0.0
  • 使用file hello_wrlinux检查文件类型,确认显示ELF 64-bit LSB executable, x86-64(或对应目标架构),而非宿主架构

常见问题

  • 编译报错“cannot find -lc”:检查Sysroot路径是否正确指向SDK的sysroot目录
  • 头文件找不到:确认Include Paths中已添加usr/include路径
  • 运行时提示“No such file or directory”:确认目标设备已安装匹配的运行时库,或使用ldd检查动态依赖

本案例完成了一个Linux用户态应用程序从工程创建、交叉编译到目标验证的完整闭环。通过getopt参数解析和/proc/version读取,验证了交叉编译产物在目标环境中的实际执行能力。