Windows系统下开发环境研究
时隔多年重新参与windows系统下的软件开发工作,现记录一下在windows下的c++开发环境搭建。
1. code
C/C++ extension tutorials per compiler and platform
- Microsoft C++ compiler (MSVC) on Windows 使用Microsoft C++编译器方案
- GCC and Mingw-w64 on Windows 使用gcc或clang编译器方案
- GCC on Windows Subsystem for Linux (WSL) 使用WSL子系统开发Linux程序的方案
- GCC on Linux
- Clang on macOS
2. gcc编译mfc程序
//main.cpp
#include <windows.h>
#pragma comment(lib,"user32.lib")
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL,"This is a test!","Title",MB_OKCANCEL);
return 0;
}
$ g++ main.cpp -o obj
问题1:
GCC编译的会弹出一个黑框框,然后再弹出对话框窗体,而cl编译的可执行程序不会弹出黑框框,直接就弹出一个对话框窗体。
解决:
ld 对应命令是 --subsystem=windows
shell
$ g++ main.cpp -o obj -mwindows
make
LDFLAGS=-mwindows
cmake
# project(${PACKAGE_NAME})
# C项目中
# error LNK2001: 无法解析的外部符号 WinMainCRTStartup
project(${PACKAGE_NAME} LANGUAGES C)
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_executable(${PACKAGE_NAME} WIN32 ${DIR_SRCS})
if(CMAKE_BUILD_TYPE STREQUAL "release")
set_target_properties(${PACKAGE_NAME} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
else()
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
endif()
else()
add_executable(${PACKAGE_NAME} ${DIR_SRCS})
if(CMAKE_BUILD_TYPE STREQUAL "release")
set_target_properties(${PACKAGE_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)
endif()
endif()
# error LNK2019: 无法解析的外部符号 WinMain
# 用 /SUBSYSTEM:CONSOLE 替换 /SUBSYSTEM:WINDOWS 并去掉 WIN32
add_executable(${PACKAGE_NAME} ${DIR_SRCS})
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
if(CMAKE_BUILD_TYPE STREQUAL "release")
set_target_properties(${PACKAGE_NAME} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")
else()
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")
endif()
else()
if(CMAKE_BUILD_TYPE STREQUAL "release")
set_target_properties(${PACKAGE_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)
endif()
endif()
c/c++
#ifdef WIN32
# include <windows.h>
# pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#endif