티스토리 뷰

 

main 함수 없이 프로그램을 실행시키면

1>------ 빌드 시작: 프로젝트: 170609 mainfunction test, 구성: Debug Win32 ------
1>main.c
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: _main 외부 기호(참조 위치: "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) 함수)에서 확인하지 못했습니다.
1>C:\Users\hison\onedrive\문서\visual studio 2017\Projects\170609 mainfunction test\Debug\170609 mainfunction test.exe : fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다.
1>"170609 mainfunction test.vcxproj" 프로젝트를 빌드했습니다. - 실패
========== 빌드: 성공 0, 실패 1, 최신 0, 생략 0 ==========

실패했다고 한다. ㅠㅠ

main 함수가 없기 때문

 

main 함수는 특수함수로서 main함수를 사용하는 모든 프로그래밍 언어의 시작점이다.

(때문에 없으면 위처럼 시작할 수 없다고 수 없다고 마구 오류를 쏟아냄)

main 함수가 있어야 실행파일을 만들 수 있다.

따라서 프로그램을 만들 때 시작점인 main함수를 적고 main 함수에 다른 함수들을 넣는 식으로 코드를 작성한다.

 

main함수는 호출당해야 하기 때문에 고정된 이름을 사용해야 하지만 매개변수, 리턴형은 고정되어있지 않다.

매개변수로 int argc, char *argv[], char *envp[] 이 세개의 인자를 넣을 수 있고

리턴형은 int, void

따라서 다음과 같은 조합이 가능하다

void main(void);

void main(int argc);

void main(int argc,char *argv[]);

void main(int argc,char *argv[],char *env[]);

int main(void);

int main(int argc);

int main(int argc,char *argv[]);

int main(int argc,char *argv[],char *env[]);

 

msdn 설명을 보면(하단 링크 또는 글)

main함수 대신에 wmain함수(UNICODE)

tchar.h 에 있는 _tmain함수도 사용할 수 있다고 한다.

 

-더보기- main함수의 return 값

-더보기- main함수의 parameter

 

MSDN 설명

main: Program Startup

 A special function named main is the starting point of execution for all C and C++ programs. If you are writing code that adheres to the Unicode programming model, you can use wmain, which is the wide-character version of main.

The main function is not predefined by the compiler. It must be supplied in the program text.

The declaration syntax for main is

int main(); 

or, optionally,
int main(int argc, char *argv[], char *envp[]); 

Microsoft Specific


The declaration syntax for wmain is as follows:

int wmain( ); 

or, optionally,

int wmain(int argc, wchar_t *argv[], wchar_t *envp[]); 

You can also use _tmain, which is defined in TCHAR.h. _tmain resolves to main unless _UNICODE is defined. In that case, _tmain resolves to wmain.

Alternatively, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system by using a return statement. To return an exit code when main or wmain is declared as void, you must use the exit function.

END Microsoft Specific


The types for argc and argv are defined by the language. The names argc, argv, and envp are traditional, but are not required by the compiler. For more information and an example, see Argument Definitions.

See Also


Keywords
Using wmain Instead of main
main Function Restrictions

 

 

http://mwultong.blogspot.com/2007/06/c-main-main-function.html

https://msdn.microsoft.com/ko-kr/library/6wd819wh.aspx

http://itguru.tistory.com/89

https://www.joinc.co.kr/w/man/12/main

http://soen.kr/lecture/ccpp/cpp1/10-4-2.htm

https://msdn.microsoft.com/en-us/library/88w63h9k(v=vs.120).aspx

http://mwultong.blogspot.com/2006/12/c-argc-argv-main-function-parameter.html

http://mwultong.blogspot.com/2006/07/error-level-errorlevel.html

http://mwultong.blogspot.com/2006/07/c-return-return-0.html

 

http://mwultong.blogspot.com/2006/07/error-level-errorlevel.html

 

 

댓글