首页 > c++ > 使用VS2015更改应用程序的入口点

使用VS2015更改应用程序的入口点 (Changing Entry Point of Application with VS2015)

问题

我试图重新定义我的应用程序的入口点不使用main()因为它与我试图测试的其他一些代码冲突。

我正在使用子系统:CONSOLE,并输出一个exe。一切都定义为

int main(int argc,CHAR  **argv) {}

事情编译得很好。

然后我把它改成了。

int main_test(int argc,CHAR  **argv) {}

在视觉工作室链接器入口点我将它从空白值更改为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作为入口点,这样我就会相信我正在做的事情是错误的。任何帮助将被赞赏似乎一个简单的问题,我不知道为什么它如此困难。

解决方法

首先,您需要设置入口点

打开项目的“属性页”对话框。有关详细信息,请参阅设置Visual C ++项目属性。

单击“链接器”文件夹。

单击“高级”属性页。

修改Entry Point属性。

并设定 main_test

之后,我不完全理解为什么,但它是必要做这个

打开项目的“属性页”对话框。有关详细信息,请参阅设置Visual C ++项目属性。

单击“链接器”文件夹。

单击“输入”属性页。

修改“强制符号引用”属性。

并设置_mainCRTStartup为x86或mainCRTStartupx64。

或者以编程方式在代码中执行:

#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,并且必须手动实现命令行参数获取等操作。有关详细信息,请参见此处

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;
}

问题

I am attempting to redefine my entry point of my application to not use main() because it is clashing with some other code that I am attempting to test.

I am using a subsystem:CONSOLE, and outputting an exe. All works when it is defined as

int main(int argc,CHAR  **argv) {}

Things compile fine.

I then change it to.

int main_test(int argc,CHAR  **argv) {}

And in the visual studio Linker entry point i change it from its blank value to main_test i start getting a ton (like 3500+) of linking errors related to libcpmtd.lib and other symbols that are missing in my obj files like __stdio_common_vsscanf which obviously is a built in function that i am not actually calling anywhere.

Error   LNK2019 unresolved external symbol ___mb_cur_max_func referenced in function _Getcvt    MyProject C:\Source\project\src\libcpmtd.lib(xwctomb.obj)   1   

I get the exact same errors if i leave my function as main shown above, and just type main in as the entry point, so it makes me believe that something im doing is wrong. Any help would be appreciated it seems like A simple problem im not sure why its so difficult.

解决方法

First you need to set 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.

and set main_test

After, I do not fully understand why, but it is necessary to do 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.

and set _mainCRTStartup for x86 or mainCRTStartup for x64.

Or do it programmatically in yor code:

#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;
}

Output

main_test

Note that main is not a default entry point. The entry point for console app is _mainCRTStartup which calls main. Therefore changing the entry point you lose CRT and you have to manually implement things like command line arguments getting. For more detailed look here

UPD

I do not think that developing your own CRT is good decision, it will be easier to use one of the built-in entry points.

#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;
}
相似信息