在另一个C文件中调用Main函数 [英] Call Main function in another C file

查看:96
本文介绍了在另一个C文件中调用Main函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两个C文件,一个是ev_test.c和capture.c,capture.c文件是从摄像机传感器捕获图像,现在,我需要从ev_test.c访问capture.c文件,

注意:

ev_test.c是按键事件中断生成器文件,因为我配置了一个key ="volumebutton",如果我按下按键"volumebutton",它必须调用capture.c文件的主要功能,并且必须捕获图像

谁能帮我在此调用主函数

注意:

如果您想要C文件,我可以给您提供参考

ev_test.c的主要功能是

hi all,

I have two C files one is ev_test.c and capture.c, the capture.c file is to capture the images from the camera sensor, Now, i need to access the capture.c file from ev_test.c,

NOTE:

The ev_test.c is key press event interrupt generator file, in that i configured the a key= "volumebutton", if i press the key "volumebutton" it must call the main function of the capture.c file and it must capture the images

Can any one help me on how to call the main function in this

NOTE:

If you want the C files i can send you for reference

THE MAIN FUNCTION OF ev_test.c is

int main (int argc, char **argv)
{
	int fd, rd, i, j, k;
	struct input_event ev[64];
	int version;
	unsigned short id[4];
	unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
	char name[256] = "Unknown";
	int abs[5];

	if (argc < 2) {
		printf("Usage: evtest /dev/input/eventX\n");
		printf("Where X = input device number\n");
		return 1;
	}

	if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
		perror("evtest");
		return 1;
	}

	if (ioctl(fd, EVIOCGVERSION, &version)) {
		perror("evtest: can't get version");
		return 1;
	}

	printf("Input driver version is %d.%d.%d\n",
		version >> 16, (version >> 8) & 0xff, version & 0xff);

	ioctl(fd, EVIOCGID, id);
	printf("Input device ID: bus 0x%x vendor 0x%x product 0x%x version 0x%x\n",
		id[ID_BUS], id[ID_VENDOR], id[ID_PRODUCT], id[ID_VERSION]);

	ioctl(fd, EVIOCGNAME(sizeof(name)), name);
	printf("Input device name: \"%s\"\n", name);

	memset(bit, 0, sizeof(bit));
	ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
	printf("Supported events:\n");

	for (i = 0; i < EV_MAX; i++)
		if (test_bit(i, bit[0])) {
			printf("  Event type %d (%s)\n", i, events[i] ? events[i] : "?");
			if (!i) continue;
			ioctl(fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
			for (j = 0; j < KEY_MAX; j++) 
				if (test_bit(j, bit[i])) {
					printf("    Event code %d (%s)\n", j, names[i] ? (names[i][j] ? names[i][j] : "?") : "?");
					if (i == EV_ABS) {
						ioctl(fd, EVIOCGABS(j), abs);
						for (k = 0; k < 5; k++)
							if ((k < 3) || abs[k])
								printf("      %s %6d\n", absval[k], abs[k]);
					}
				}
		}
		

	printf("Testing ... (interrupt to exit)\n");

	while (1) {
		rd = read(fd, ev, sizeof(struct input_event) * 64);

		if (rd < (int) sizeof(struct input_event)) {
			printf("yyy\n");
			perror("\nevtest: error reading");
			return 1;
		}
		printf("OVIYA RD=%d SIZE=%d\n",rd, sizeof(struct input_event));
		for (i = 0; i < rd / sizeof(struct input_event); i++)
			
			if (ev[i].type == KEY_VOLUMEDOWN) {
			
				printf("Event: time %ld.%06ld, -------------- %s ------------\n",
					ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
			} else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
				printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
					ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
					events[ev[i].type] ? events[ev[i].type] : "?",
					ev[i].code,
					names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
					ev[i].value);
			} else {
				printf("OVIYA CHECK3--------------\n");
				printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
					ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
					events[ev[i].type] ? events[ev[i].type] : "?",
					ev[i].code,
					names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
					ev[i].value);
			}	

	}
}



谢谢


添加了代码块,并删除了标题中的内容[/编辑]



Thanks
shan

Code block added and shouting from title removed[/Edit]

推荐答案

调用另一个文件中定义的"C"函数而无需常见的头文件是将其声明为extern.

The usual way to call a ''C'' function defined in another file without a common header file is to declare it as extern.

//file1.c

//declare a ''foreign'' function which is not defined

extern int OtherFunction( char**, int );

//implement the functions that belong in this file scope

int MainFunction1( void )
{
//The declared but not implemented ''OtherFunction'' can be called here

  int iResult = OtherFunction( &aString, iCountStrings );
//...
}



file1.c代码将编译,但不会链接,因为它依赖于尚未定义的外部函数.因此,现在可以添加file2.c来实现缺少"功能.



The file1.c code will compile but it won''t link because it relies on an external function that hasn''t been defined. So now file2.c can be added which implements the ''missing'' function.

//file2.c

int OtherFunction( char** aStrings, int iCountStrings )
{
//...
}



file2.c会自行编译,然后输出与编译file1.c的输出链接,以形成一个可执行文件,该链接成功链接,因为现在已经存在所有必需的内容.

我应该希望显而易见的是,如何将这种模式应用于您的情况,以启用从另一个文件中的另一个文件作用域函数的调用.



The file2.c is compiled on its own and then the output linked with the output of compiling file1.c to form a single executable which links successfully because everything necessary is now present.

It should I hope be obvious how to apply this pattern to your situation to enable the calling of one file scope function from another in a different file.


为什么不以完整的方式调用可执行文件命令行来完成这项工作?

否则,在解析器和调用函数中清理main函数.这样可以更好地访问call函数.
Why arent you call the executable with a complete commandline to do the job?

Otherwise cleanup the main function in a parser and call function. The call function is then better accessable.


这篇关于在另一个C文件中调用Main函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆