VxWorks入门教程13:程序运行-启动时

系列索引:VxWorks入门教程索引

上一篇:VxWorks入门教程12:程序运行-启动后

本文介绍在VxWorks启动时运行开发的程序。

按照正常流程创建引导镜像和系统镜像。

我们需要修改系统镜像部分的代码

基础版

在镜像工程中添加两个文件hello.cshowarray.cpp

hello.c内容为

1
2
3
4
5
6
7
#include <stdio.h>

int first_test(void)
{
printf("hello world!\n");
return 0;
}

shwoarray.cpp内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vxworks.h> 
#include <iostream>

using namespace std;

extern "C" void test(){
int i=1;

while(1){
cout<<i;
if(i%10==0 && i!=0){
cout<<endl;
}
i++;
if(i==101){
break;
}
}
cout<<endl;
}

在工程中的usrAppInit.c的usrAppInit函数中添加

1
2
3
4
5
6
7
8
9
10
void usrAppInit (void)
{
#ifdef USER_APPL_INIT
USER_APPL_INIT; /* for backwards compatibility */
#endif

first_test();
test();
/* add application specific code here */
}

效果为:

project

运行效果为:

result

很明显,数组输出时没有空格。

这里要注意的是,C++代码如何编译

在函数前加上

1
extern "C"...

进阶版

上面一种是将程序集成到镜像中,本部分介绍镜像自动调用程序。

首先准备一个用于调用的程序。

1
2
3
4
5
6
7
#include <stdio.h>

int first_test(char* a)
{
printf("%s\n",a);
return 0;
}

输入一个字符串,此函数将输入的字符串输出。

以下所有的操作都是在usrAppInit.c中操作

首先是添加头文件

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <taskLib.h>
#include <stdlib.h>
#include <string.h>
#include <symLib.h>
#include <moduleLib.h>
#include <loadLib.h>
#include <fcntl.h>

加载模块(打开文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
static STATUS _load_module(char* filename){
int fd;
MODULE_ID hModule;

/*open app*/
if((fd=open(filename,0,0664))==ERROR){
printf("fd = %d\n", fd);
printf("Cannot open %s(AppRun) file.!\n", filename);
return ERROR;
}

/*load all symbol table*/
hModule = loadModule(fd, LOAD_ALL_SYMBOLS);
close(fd);
return OK;
}

加载程序、查找函数,调用函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
STATUS load_app(char *filename, char* main_name){
SYM_TYPE p_type;
FUNCPTR main_func = NULL;

if(_load_module(filename)==ERROR){
printf("load_module (%s) error\n.",filename);
}

/*find symbol*/
STATUS status = symFindByName(sysSymTbl,main_name,(char*)&main_func,&p_type);
if (status == ERROR)
{
printf("symFindByName error\n");
return 1;
}else{
printf("deviceEntry = 0x%x, type = %d\n", (int)main_func, (int)p_type);
char* szPara = "test for booting run!";
(*main_func)(szPara);
}

return OK;
}

vxworks提供了统一的调用接口

1
2
3
4
5
6
7
8
9
void usrAppInit (void)
{
#ifdef USER_APPL_INIT
USER_APPL_INIT; /* for backwards compatibility */
#endif
printf("app run...\n");
load_app("/ata0a/hello.out","first_test");
/* add application specific code here */
}

运行效果为:

result

相关文件在VxWorks_Freshman的8中。

下一篇:VxWorks入门教程14:系统编程方法


VxWorks入门教程13:程序运行-启动时
https://feater.top/vxworks/run-application-at-vxworks-booting/
作者
JackeyLea
发布于
2021年11月30日
许可协议