Рассмотрим следующий сценарий CMake и CPP исходный файл.
cmake_minimum_required(VERSION 3.15)
project(dummy)
add_executable(dummy WIN32)
target_compile_features(dummy
PRIVATE cxx_std_17)
target_link_options(dummy PRIVATE
/ENTRY:mainCRTStartup) #< Get rid of WinMain.
target_sources(dummy PRIVATE
dummy_test.cpp)
#include <windows.h>
#include <cassert>
namespace {
LRESULT
__stdcall windows_procedure(HWND window_handler, UINT message, WPARAM word_param, LPARAM long_param) {
return ::DefWindowProc(window_handler, message, word_param, long_param);
}
}
int main(int argc, char** argv)
{
void* native_handle;
auto width = 1024;
auto height = 768;
unsigned int window_style =
static_cast<unsigned int>(WS_OVERLAPPEDWINDOW) |
static_cast<unsigned int>(WS_VISIBLE);
RECT window_rect = {0, 0, width, height};
assert(::AdjustWindowRect(&window_rect, window_style, false));
WNDCLASS window_class = {};
window_class.style = (unsigned int) CS_HREDRAW | (unsigned int) CS_VREDRAW | (unsigned int) CS_OWNDC;
window_class.lpfnWndProc = ::windows_procedure;
window_class.hInstance = ::GetModuleHandle(nullptr);
window_class.hIcon = ::LoadIcon(nullptr, IDI_APPLICATION);
window_class.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
window_class.hbrBackground = ::CreateSolidBrush(RGB(0U, 0U, 0U));
window_class.lpszMenuName = nullptr;
window_class.lpszClassName = "window";
assert(::RegisterClass(&window_class));
native_handle = ::CreateWindow("window",
"Window",
window_style,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
nullptr,
nullptr,
GetModuleHandle(nullptr),
nullptr);
assert(native_handle); //< Unable to CreateWindow;
for(;;) {}
}
Программа компилируется и работает просто замечательно! Рассмотрим следующую вещь. Я использовал Windows функции из User32.lib Winmm.lib и Win32.lib И я не предоставил их для связи! Вот сборка "log".
add_executable (dummy WIN32) - здесь Win32 не имеет ничего общего с моей проблемой. Это работает с или без него.
PS C:\Users\Vlad\Documents\Projects\Cpp\test> cmake .
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.25.28614.0
-- The CXX compiler identification is MSVC 19.25.28614.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Vlad/Documents/Projects/Cpp/test
PS C:\Users\Vlad\Documents\Projects\Cpp\test> cmake --build .
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
Building Custom Rule C:/Users/Vlad/Documents/Projects/Cpp/test/CMakeLists.txt
dummy_test.cpp
dummy.vcxproj -> C:\Users\Vlad\Documents\Projects\Cpp\test\Debug\dummy.exe
Building Custom Rule C:/Users/Vlad/Documents/Projects/Cpp/test/CMakeLists.txt
PS C:\Users\Vlad\Documents\Projects\Cpp\test> cmake --version
cmake version 3.17.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
PS C:\Users\Vlad\Documents\Projects\Cpp\test>
MSV C всегда жаловался на неразрешенные внешние символы из User32, Winmm или других Windows, указанных c libs ранее. (Сейчас это не жаловалось даже на Xinput). Итак, мой вопрос : это новое поведение CMake или MSV C (принудительная связь с Windows)? Как я могу избавиться от этого? Я хочу связать вручную и импортировать некоторые функции во время выполнения.