大佬教程收集整理的这篇文章主要介绍了有没有办法在 main() 函数之前使 loading() void 开始?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对 C 非常陌生,我正在尝试创建一种在实际程序运行之前出现的加载屏幕。我编译了它,它完美地运行了一次,但在那之后它不会再运行了。我想知道我是否搞砸了什么?我不想在主函数中使用它的原因是因为我想允许返回主菜单而不必再次输入名称。任何帮助都会很棒,就像我说我对 C. P.s. 很陌生。如果需要,我可以附上头文件。我不确定是什么问题。
主文件:
#include "perioDic-header.h"
voID blue() {
printf("\033[0;36m");
}
voID lime() {
printf("\033[1;32m");
}
voID yellow() {
printf("\033[1;33m");
}
voID colorreset() {
printf("\033[0m");
}
loading();
int main(char *userName) {
int choice;
colorreset();
system("cls");
table();
printf("\n\n");
yellow();
printf("\t\t Using the numbers on your keyboard,\n");
printf("\t\t Please SELEct an option:\n\n");
printf("\t\t 1) examine element\n");
printf("\t\t 2) input data for element\n");
printf("\t\t 3) Exit program\n");
choose(&choice,3);
switch(choicE){
case 1:{
examineElement();
break;
}
case 2:{printf("\t\t Test 2\n");
// addElement();
break;
}
case 3:{
system("cls");
printf("\n\n\t\t Thanks for stopPing by %s,\n",userName);
Sleep ( 300 );
printf("\t\t ... hope to see you again soon!\n");
colorreset();
exit(1);
break;
}
default: printf("\t\t Sorry %s,that is not a valID option",userName); break;
}
colorreset();
}
加载文件:
#include "perioDic-header.h"
voID loading(){
system("cls");
yellow();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t [ ASPECT ]\n");
colorreset();
blue();
Sleep ( 10000 );
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t Loading...\n");
Sleep ( 900 );
colorreset();
yellow();
system("cls");
printf("\n\n");
printf("\t\t Welcome...\n");
Sleep ( 300 );
printf("\t\t Please enter your name: ");
char username [20];
fscanf(stdin,"%20s",userName);
Sleep ( 1300 );
}
头文件:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#include <String.h>
#include <stdbool.h>
extern char username[20];
// Prints vIEw of perioDic table
voID table();
// Manipulates perioDic table
voID examineElement();
voID addElement();
//Not in use yet
voID menuSELEction();
char SELEction[2];
// Used to choose what you want to do with the program
int choose();
// Element Credentials
extern char elementname[50];
extern char sb[5];
extern int atm;
extern float atms;
extern char block;
extern char atc[20];
extern char prop[250];
不幸的是,这是不可能的,因为 @H_622_7@main() 是程序的入口点,这意味着它是在您运行程序时执行的函数。但是,您可以做的是将 main 的内容放在一个单独的函数中,并在程序开始之前在 main 中调用 loading()
。这就是我所指的:
void load() {
// loading screen
}
void program() {
// the program
}
int main() {
load();
program();
}
,
我们可以使任何函数在调用@H_622_7@main之前运行如果我们使它成为一个构造函数。
构造函数可以采用“优先级”编号,因此我们可以有多个以特定顺序运行。
较低优先级的数字是为 gcc/glibc
内部回调函数保留的,因此我们应该添加一个偏差,以免与它们发生冲突。
同样,我们可以设置一个“析构函数”。
这是一些示例代码:
#include <stdio.h>
#define ctors ctorx(0)
#define dtors dtorx(0)
#define ctorx(_prio) __attribute__((constructor(_prio + 201)))
#define dtorx(_prio) __attribute__((destructor(_prio + 201)))
void ctorx(0)
loading(void)
{
printf("%s ...\n",__FUNCTION__);
}
void ctorx(-1)
i_am_called_before_loading(void)
{
printf("%s ...\n",__FUNCTION__);
}
void ctorx(1)
i_am_called_after_loading(void)
{
printf("%s ...\n",__FUNCTION__);
}
void dtorx(0)
i_am_called_after_main_has_completed(void)
{
printf("%s ...\n",__FUNCTION__);
}
int
main(void)
{
printf("main: running ...\n");
return 0;
}
这是程序的输出:
i_am_called_before_loading ...
loading ...
i_am_called_after_loading ...
main: running ...
i_am_called_after_main_has_completed ...
更新:
你能解释一下如何在 C 中创建构造函数吗?我的猜测是,如果没有 C++ 编译器,您的代码将无法运行。 – 布兰登
不,无论如何这都会起作用。
这是 gcc
的一部分。有关详细信息,请参阅 info gcc
。
AFAICT,这 [直接] 使用了 c++
将用于静态/全局构造函数/析构函数的 [底层] 机制。也就是说,c++
并不“拥有”该机制,而只是 ELF 提供的更通用机制的客户端,并且可以被各种不同的语言使用。
这是在过去几年中添加的。
在此之前,可以使用 ELF 的 .init/.fini
部分属性来存储指向回调函数的指针。
这就是共享库(.so
文件)如何获得对 [隐藏] 初始化函数的回调。 ELF 解释器(例如 /lib64/ld-linux-x86-64.so
)将在加载库时查找并发出回调。
或者,对于非常旧的代码,init/fini 函数必须被称为 init
和 fini
。为此,我们可能不得不将这些函数放入一个单独的共享库 (.so
) 中,但现在情况不再如此。
AFAIK,旧机制仍然可用/可用/有效,并且可以与 constructor
属性并行使用。
我不确定gcc
如何实现constructor
属性,但这是一个猜测:
该属性被变形为一个函数指针,该指针被放入(例如)section(gcc_constructor_list)
。 crt0.o
[或等效的] 例程将调用该部分中定义的内容。
另一种方法是生成指向函数的指针并使用 ELF 的 .init/.fini
部分。
或者,发出对 gcc_constructor_list
调用的函数可能只是一个 ELF .init
函数(因此,根本不需要在 crt0.o
中)。
要查看实际生成的内容,在构建程序后,我们可以执行 readelf -a ./program
并在输出中有:
.init
和 .fini
部分标题。 init_array
和 .fini_array
而且,“动态部分”中的一些条目:INIT_ARRAY
、INIT_ARRAYSZ
、FINI_ARRAY
和 FINI_ARRAYSZ
以上是大佬教程为你收集整理的有没有办法在 main() 函数之前使 loading() void 开始?全部内容,希望文章能够帮你解决有没有办法在 main() 函数之前使 loading() void 开始?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。