使用 VS2015 更改应用程序的入口点
Changing Entry Point of Application with VS2015
我正在尝试重新定义我的应用程序入口点以不使用 main(),因为它与我正在尝试测试的其他一些代码冲突。
我正在使用 subsystem:CONSOLE,并输出一个 exe。定义为
时一切正常
int main(int argc,CHAR **argv) {}
编译正常。
然后我将其更改为。
int main_test(int argc,CHAR **argv) {}
并且在 visual studio 链接器入口点我将其从空白值更改为 main_test 我开始收到大量(如 3500+)与 libcpmtd.lib 相关的链接错误和我的 obj 文件中缺少的其他符号,如 __stdio_common_vsscanf,这显然是一个内置函数,我实际上并没有在任何地方调用。
Error LNK2019 unresolved external symbol ___mb_cur_max_func referenced in function _Getcvt MyProject C:\Source\project\src\libcpmtd.lib(xwctomb.obj) 1
如果我将我的函数保留为上面所示的 main,并且只键入 main 作为入口点,我会得到完全相同的错误,所以这让我相信我做的事情是错误的。任何帮助将不胜感激,这似乎是一个简单的问题,我不确定为什么如此困难。
首先你需要设置entry point
Open the project's Property Pages dialog box. For details, see Setting
Visual C++ Project Properties.
Click the Linker folder.
Click the Advanced property page.
Modify the Entry Point property.
并设置main_test
之后,我不完全明白为什么,但是有必要做this
Open the project's Property Pages dialog box. For details, see Setting
Visual C++ Project Properties.
Click the Linker folder.
Click the Input property page.
Modify the Force Symbol References property.
并为 x86 设置 _mainCRTStartup
或为 x64 设置 mainCRTStartup
。
或者在您的代码中以编程方式进行:
#pragma comment(linker, "/ENTRY:main_test")
#if defined(_M_IX86)
# pragma comment(linker, "/INCLUDE:_mainCRTStartup")
#else
# pragma comment(linker, "/INCLUDE:mainCRTStartup")
#endif
#include <iostream>
int main() {
std::cout << "main" << std::endl;
return 0;
}
int main_test()
{
std::cout << "main_test" << std::endl;
return 0;
}
输出
main_test
请注意 main
不是默认的 入口点 。控制台应用程序的入口点是 _mainCRTStartup
,它调用 main
。因此,更改入口点会丢失 CRT,您必须手动实现命令行参数获取之类的操作。更详细的看here
UPD
我不认为开发自己的 CRT 是个好决定,使用内置入口点之一会更容易。
#pragma comment(linker, "/ENTRY:wmainCRTStartup ") // wmain will be called
//#pragma comment(linker, "/ENTRY:mainCRTStartup ") // main will be called
#include <iostream>
#include <stdlib.h>
int main(int argc, char** argv) {
for (int i = 0; i < argc; i++)
std::cout << argv[i] << std::endl;
return 0;
}
int wmain(int argc, wchar_t *argv[])
{
for (int i = 0; i < argc; i++)
std::wcout << argv[i] << std::endl;
return 0;
}
我正在尝试重新定义我的应用程序入口点以不使用 main(),因为它与我正在尝试测试的其他一些代码冲突。
我正在使用 subsystem:CONSOLE,并输出一个 exe。定义为
时一切正常int main(int argc,CHAR **argv) {}
编译正常。
然后我将其更改为。
int main_test(int argc,CHAR **argv) {}
并且在 visual studio 链接器入口点我将其从空白值更改为 main_test 我开始收到大量(如 3500+)与 libcpmtd.lib 相关的链接错误和我的 obj 文件中缺少的其他符号,如 __stdio_common_vsscanf,这显然是一个内置函数,我实际上并没有在任何地方调用。
Error LNK2019 unresolved external symbol ___mb_cur_max_func referenced in function _Getcvt MyProject C:\Source\project\src\libcpmtd.lib(xwctomb.obj) 1
如果我将我的函数保留为上面所示的 main,并且只键入 main 作为入口点,我会得到完全相同的错误,所以这让我相信我做的事情是错误的。任何帮助将不胜感激,这似乎是一个简单的问题,我不确定为什么如此困难。
首先你需要设置entry point
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Advanced property page.
Modify the Entry Point property.
并设置main_test
之后,我不完全明白为什么,但是有必要做this
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Input property page.
Modify the Force Symbol References property.
并为 x86 设置 _mainCRTStartup
或为 x64 设置 mainCRTStartup
。
或者在您的代码中以编程方式进行:
#pragma comment(linker, "/ENTRY:main_test")
#if defined(_M_IX86)
# pragma comment(linker, "/INCLUDE:_mainCRTStartup")
#else
# pragma comment(linker, "/INCLUDE:mainCRTStartup")
#endif
#include <iostream>
int main() {
std::cout << "main" << std::endl;
return 0;
}
int main_test()
{
std::cout << "main_test" << std::endl;
return 0;
}
输出
main_test
请注意 main
不是默认的 入口点 。控制台应用程序的入口点是 _mainCRTStartup
,它调用 main
。因此,更改入口点会丢失 CRT,您必须手动实现命令行参数获取之类的操作。更详细的看here
UPD
我不认为开发自己的 CRT 是个好决定,使用内置入口点之一会更容易。
#pragma comment(linker, "/ENTRY:wmainCRTStartup ") // wmain will be called
//#pragma comment(linker, "/ENTRY:mainCRTStartup ") // main will be called
#include <iostream>
#include <stdlib.h>
int main(int argc, char** argv) {
for (int i = 0; i < argc; i++)
std::cout << argv[i] << std::endl;
return 0;
}
int wmain(int argc, wchar_t *argv[])
{
for (int i = 0; i < argc; i++)
std::wcout << argv[i] << std::endl;
return 0;
}