关于在windows编译出exe老是弹出cmd窗口的问题

1.首先遇到问题,先找答案,然后拼命试试。

2.我没有看自己的环境,编译环境等等,首先,我说明一下,这里是用CMake 编译出来的exe可执行文件.

3.现在这里用的c++. (MinGW)

进入正题:

a.  首先搜索 怎么隐藏cmd窗口,(之前在用python打包exe的时候,遇到过,打包完成的python程序,变成exe 后,点击exe 会有黑色的弹框,也是cmd 窗口)。

用一个Pyinstaller 工具打包的,添加一个-F 编译选项即可 解决了。

我TM 无语了,我程序本来是python写的,因为某些原因改用c++写了,你说要我 用回之前的python语言写?我不,就不。

于是,就找到了这些链接:

隐藏exe、Win32控制台程序运行

无非就是这些:

#include<windows.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址
int main()
{
    MessageBox(NULL,"Hello","Notice",NULL);
    return 0;
}

试过了,没有用 的。这个适合,在vs 上编译的exe

还有就是这个,

使用api函数隐藏:

#include<windows.h>
int main()
{
    HWND hwnd;
    hwnd=FindWindow("ConsoleWindowClass",NULL); //处理顶级窗口的类名和窗口名称匹配指定的字符串,不搜索子窗口。
    if(hwnd)
    {
        ShowWindow(hwnd,SW_HIDE);               //设置指定窗口的显示状态
    }
    MessageBox(NULL,"Hello","Notice",MB_OK);
    system("pause");
    return 0;
}

我 程序里面,什么都没有调用,都会出现那个 黑色的cmd 窗口, 还是没法解决的啊。

........

......

后面,我觉得是要在编译器 上面下功夫,是要告诉编译器,有些东西不要生成。

找了好 几个小时。

https://stackoverflow.com/questions/18260508/c-how-do-i-hide-a-console-window-on-startup

在这个解决方案中,有人回答了,类似这个:

Hiding a console window at startup is not really possible in your code because the executable is run by the operating system with specific settings. That's why the console window is displayed for a very short time at startup when you use for example FreeConsole(); To really hide the window at startup, you have to add a special option to you compiler. If you use gcc on Windows (MinGW) you can just add -mwindows as compiler option in your makefile and there will be absolutely no window or "flash". I don't know about VisualStudio or whatever you use at the moment, but changing the way your IDE compiles you code is the way to go instead of coding workarounds in C++.

In my view, this approach is better than using WinMain because it works reliably and you don't make your C++ Code platform dependent.

这个 意思是说,添加一个编译选项,-mwindows 

我在CMakeLists 文件中添加了,好像没什么作用,那个框,还是弹出来了,真是无语...

到后来又过了一个小时,我看到了这个:

https://www.codenong.com/2752792/

add_executable(Cmd WIN32 cmd.c)

需要在生成可执行文件的时候,添加 WIN32 关键字.

不然上面的-mwindows 参数 ,好像也不会生效啊.

至此,问题已经解决。

o yeah  ~~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Teleger

你的支持是我前进的方向

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值