deargui-vpl/docs/console-variants.md

106 lines
3.4 KiB
Markdown

# Console Application Variants
## Overview
The project now supports building both GUI and console variants of applications on Windows. This is useful for debugging, automation, or when you need console output visible by default.
## Build Configuration
### CMake Option
The `BUILD_CONSOLE_VARIANTS` option (default: `ON`) controls whether console variants are built:
```bash
# Build with console variants (default)
cmake -S examples -B build -G "Visual Studio 16 2019" -A x64
# Build without console variants
cmake -S examples -B build -G "Visual Studio 16 2019" -A x64 -DBUILD_CONSOLE_VARIANTS=OFF
```
### What Gets Built
When `BUILD_CONSOLE_VARIANTS=ON` on Windows:
- **GUI Application**: `blueprints-example_d.exe` (Debug) / `blueprints-example.exe` (Release)
- Windows GUI application (subsystem: WINDOWS)
- Console attaches to parent process if launched from terminal
- Creates its own console window if no parent console exists
- **Console Application**: `blueprints-example-console_d.exe` (Debug) / `blueprints-example-console.exe` (Release)
- Windows console application (subsystem: CONSOLE)
- Always shows console window
- Better for debugging and automation
## Usage
### Running the Console Variant
From PowerShell:
```powershell
# Build
sh ./scripts/build.sh
# Run console variant
./build/bin/blueprints-example-console_d.exe
# Run with arguments
./build/bin/blueprints-example-console_d.exe --file "myfile.json"
```
From Command Prompt:
```cmd
build\bin\blueprints-example-console_d.exe
```
### Visual Studio
Both targets appear in the solution (`build\imgui-node-editor.sln`):
- `blueprints-example` - GUI application
- `blueprints-example-console` - Console application
**By default, `blueprints-example-console` is set as the startup project**, so you can just press F5 to debug the console variant.
To change the default startup project:
```bash
# Use GUI variant as startup project
cmake -S examples -B build -DUSE_CONSOLE_AS_STARTUP=OFF
# Use console variant as startup project (default)
cmake -S examples -B build -DUSE_CONSOLE_AS_STARTUP=ON
```
Or manually in Visual Studio: Right-click either project and select "Set as StartUp Project".
## Differences
| Feature | GUI Variant | Console Variant |
|---------|-------------|-----------------|
| Subsystem | WINDOWS | CONSOLE |
| Console Window | Only if attached/allocated | Always visible |
| Entry Point | WinMain() | main() |
| Use Case | Normal usage | Debugging, automation |
| Logging | Visible if launched from console | Always visible |
## Implementation Details
- Both variants share the same source code
- The console variant defines `_CONSOLE`, which triggers the `main()` entry point in `entry_point.cpp`
- The GUI variant uses `WinMain()` and includes logic to attach/allocate console when needed
- All command-line arguments work the same in both variants
## When to Use Console Variant
- **Debugging**: See printf/std::cout output immediately
- **Automation**: Easier to capture stdout/stderr in scripts
- **Testing**: Better integration with CI/CD pipelines
- **Development**: Quicker access to logs without checking files
## When to Use GUI Variant
- **Distribution**: Normal end-user deployment
- **Production**: Cleaner user experience without console window
- **Default**: Standard usage for GUI applications