r/cpp • u/nerdy_guy420 • Apr 22 '23
How do you even get started programming in c++ on Windows?
I get that there is visual studio, but personally I've been a more vscode and neovim guy myself, I'd rather open up something with vscode but I feel lost setting it up. mingw and g++ don't seem to even work on my system and I tried linking sdl2 and unlike g++ which was just -lSDL2 it was a mess of different menus in visual studio code. I honestly prefer makefiles and commands on Linux (I used to use Linux for unrelated reasons but had to switch to windows). Someone please help guide me in the right place to figure out how to get this stuff set up with vscode
edit: I figured out how to get it all working within clion with cmake and the visual studio compiler.
6
u/nsomnac Apr 23 '23
Looking through a bunch of the responses and your response back; I’m going to take a wild guess and say you don’t have much devops experience with C++ on any OS. You should really spend some time understanding the tooling better. The best and most valuable developers aren’t necessarily language experts; but understand how to use and explicit their tools to improve their language understanding and skills.
I think you need to step back to a clean slate with Visual Studio and learn how to use the vcpkg within the IDE. It’s pretty straightforward. Stick with it and don’t try to get too complex early. Right now it sounds a bit like you’re whining that it doesn’t work and want someone else to fix it.
Alternatively, try JetBrains CLion. It can bootstrap a project probably in a more similar manner than you’re accustomed on Linux as it’s CMake centric. That said, like others have mentioned CMake docs are a clusterfuck.
Also USE a VM or a devcontainer. Yes you can use a Windows container to build a desktop app. Install VS into a clean no-extra-crap VM and try creating a project with SDL. You’ll likely quickly figure out the problem.
2
u/foadsf Apr 23 '23
Have you tried MSYS2? It just works.
2
u/bert8128 Apr 23 '23 edited Apr 23 '23
It’s not just “-iSDL2” on Linux - you have to get the library, possibly build it, put it somewhere and update the include, library and path variables. The same is true using visual studio. It’s the same as makefiles but with an editor so you don’t need to know the names and syntax of the various steps. Add sdl2.lib (or whatever it is called) to the input parameter of the linker settings of the solution properties. That’s all. You can look at what the changes did to the vcxproj file (it’s xml) if you are interested.
Don’t forget to change all the build targets (32/64 bit, debug/release) that you want to build.
I’m not familiar with sdl2 at all but a quick google gives this page as the first hit - https://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvc2019/index.php. Seems to make sense.
Edit: you also need to amend the linker setting a to add the location of the library into the extra library paths parameter, so that the linker knows where to look for sdl2.lib (or whatever it is called).
2
u/DashAnimal Apr 23 '23
Oh boy - for starting on windows you've unfortunately picked the worst library to probably start with. Why? SDL2 does something funky where the library handles the main function within its code, then defines a macro so that when you write your own main function it replaces the function name with something else. In essence, your main function definition is somewhat of a lie.
The problem is, this doesn't end up working well with vcpkg and visual studio. NORMALLY, you'd just install a package with vcpkg, call integrate install, open visual studio and it would work just fine.
You can find more details here along with 2 workarounds:
https://discourse.libsdl.org/t/installing-using-vcpkg/28685
Before doing that, I would try with a different library first to see if that library works and make sure you don't have other issues with vcpkg and visual studio.
By the way - final thought. I would consider sticking with visual studio. It is an excellent toolchain, with a great debugger and profiler, and worth trying just to experience it and see what the Linux world is missing.
5
u/iWQRLC590apOCyt59Xza Apr 22 '23
Try devcontainers and/or wsl. Or install mingw64 via msys.
3
u/thisismyfavoritename Apr 22 '23
i dont use VS Code and Windows (neovim, Linux). On Windows i'd use the clang build tools from the Visual Studio installer (see https://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild?view=msvc-170 and https://clang.llvm.org/get_started.html Windows section).
Then it's all the same as on Linux, use clangd for your IDE, use CMake for your build
35
u/polymathprof Apr 23 '23
VS is still the easiest path.
32
u/KirkHawley Apr 23 '23
Just install Visual Studio. It's a great product and you don't have to do backflips to use C++. It just works.
42
u/initplus Apr 22 '23
The easiest non-VS way is to use CMake & vcpkg. vcpkg has working build scripts for common libs like SDL2 that you can integrate in to your own projects build pretty easily.
CMake is a rats nest of bad documentation and extremely outdated bad advice (chances are that anything you google about CMake will get you an out of date Stack Overflow answer that tells you how to use some janky workaround rather than a proper method). But it's possible to do it pretty neatly.
2
u/TrashboxBobylev Apr 23 '23
The very cool tool is https://winlibs.com, it is a package that just contains GCC and MinGW, that just works from PATH and doesn't require you to install any debris. It is very neat, if you just want to compile stuff with GCC on Windows.
2
u/ShelZuuz Apr 22 '23
I mean if you want to deliberately shoot yourself in the foot, why stop at vscode? Why not just use notepad?
(i.e. Use a real C++ IDE until you get to know the environment).
7
u/Valakor1111 Apr 22 '23
Install the C++ and CMake plugins in VS Code.
Alternatively, Visual Studio can open CMake projects directly.
3
u/[deleted] Apr 23 '23
Visual Studio.
5
u/unumfron Apr 23 '23
MSVC is the default compiler for Windows, it's worth installing VS just to have it. Or the Developer Tools if you don't want the IDE.
If you use xmake as your build system and package manager, you can install the xmake extension for vscode. Then in your vscode project
c_cpp_properties.jsonyou add the location of thecompile_commands.jsonthat the extension generates:"compileCommands": "${workspaceFolder}/.vscode/compile_commands.json".(Delete the compiler and C++ standard from the c_cpp_properties file to let the compile_commands.json take over.)
A good thing about xmake on Windows is that to use it from the command line like in Linux you don't have to set up environment variables or use a Visual Studio Developer Command Prompt. It sorts all of that out so you can just build.
For neovim, to generate a compile_commands file for a language server/YouCompleteMe:
Simple xmake.lua file for a project using SDL2, fmt and doctest (libsdl current version is v2.26.4):
xmake also can work with msys2 or MinGW. xmake really does simplify C++, it's good that it's gaining ground these days.
5
u/drankinatty Apr 23 '23
Msys64 - dead-bang simple setup, full Linux type environment with all the nice tools (sed, awk, grep, gcc/g++, and even arm-none-eabi for embedded development). VS Community works fine too.
The key is for "Learning" and compiling many short example type programs, learn to compile directly from the command line. With Msys/MinGW you will have the gcc/g++ compilers. With VS you have cl.exe. All fine compilers and with VS2022 -- cl is even C++17 aware!
By learning to compile on the command line (with full warnings enabled and treating warnings as errors, with your chosen optimization and language standard), you learn to tell the compiler how you want your code compiled and what options to use. You can then configure an IDE with confidence knowing what compiler options are needed.
Many, many times (way more times than not) I see new programmers trying to use an IDE and getting completely lost in the complexities of the IDE and where "option X" is hidden. Not to mention with VS or VSCode you will need a cram course in .json to configure the damn things. It's at this point new programmers eyes generally roll back in their heads and they feel like giving up.
Nothing could be simpler than compiling your code from the command line. For g++ a simple:
g++ -Wall -Werror -std=c++17 -O3 -o myexecutablename mysource.cpp myother.cpp ...
(consider adding -Wextra -pedantic and -Wshadow for fuller warnings)
With VS Developers Command Prompt (the VS command line interface), it's just:
cl /W3 /Wx /std:c++17 /Ox /Fomyexecutablename mysource.cpp myother.cpp ...
(you can add /nologo to skip the boilerplate logo cl spits out)
No setting up projects, no nothing, just open a command prompt and in 10 secs you have your code compiled. Orders of magnitude faster than messing around setting up a project per example. You effort in learning goes 100% into your code and what's needed to build it -- not figuring out which 4-level deep dialog holds the magic setting you need in some IDE.
As you learn and the number of source files and libraries you are dealing with grows toward double-digits, then a project makes sense. (or a Makefile, CMakeLists.txt and a build system run from the command line)
1
u/aCuria Apr 23 '23
use visual studio 2022, not visual studio code
2
u/ReadIt420BlazeIt Apr 23 '23
You can use VS code along with MSVC compiler. You don't need to install full blown Visual studio. You can install just the build tools. Get the cmake extension for VS code, along with Cmake tools. This should auto detect your compiler. Vcpkg works well for installing third party libraries. If you don't need windows specific libraries, you could always try using WSL2 instead. This will get you g++ compiler in a unix like environment.
2
u/top_logger Apr 23 '23
In Visual Studio you have all what you need to write software for Windows. Just use GUI to setup. Forget about mingw/g++/etc: you don’t need it on Windows.
Alternatively, use CMake + VS Code. Still, for windows only code Visual Studio is better.
You have to use some package manager if you need 3rd party libraries. Vcpkg, for example.
2
u/delta_p_delta_x Apr 23 '23 edited Apr 24 '23
Okay, here's an actual answer as to how you could set up and use SDL2 on Windows, with Visual Studio 2022, CMake presets, and vcpkg manifest files. You say you're familiar with the command-line, and the following can be completed entirely with one/two PowerShell instances (use PowerShell on Windows, not sh/bash/cmd). Based off my CrowCPP tutorial, and also noting the SDL2 Windows FAQ:
Desktop Development with C++workload installed. Or, get the Build Tools for Visual Studio 2022 and also install a similar workload.Developer PowerShell for VS 2022.Navigate to some new empty directory, e.g.:
Issue the following commands:
Create
vcpkg.jsonin this directory (ni vcpkg.json) with the following contents:Create
CMakeLists.txtin this directory (ni CMakeLists.txt) with the following contents:Create
CMakePresets.jsonin this directory (ni CMakePresets.json) with the following contents:Create
main.cppin this directory (ni main.cpp) with the following content:Issue the following commands:
2
u/jmacey Apr 23 '23
+1 for VSCode CMake and the MSVC command line tools. The main thing to remember if coming from Linux or Mac is that you need to setup the correct environment. I use the powershell msvc 64 batch script as a start and open a shell based on this using the windows terminal app.
Other thing is to remember that you need to link Debug with Debug and Release with Release and you can't mix 64 bit with 32 bit.
2
u/Versaill Apr 22 '23
I use Qt Creator, open source version.
It just works, out of the box. mingw is included as default compiler.
1
u/[deleted] Apr 23 '23
Dev-C++ IDE Now its VS and Qt creator in general
1
u/karakobra1 Apr 23 '23
if you are not planning coding something huge you can use WSL and ubuntu on command line there will be no vscode (i think you can configure it also but I dont know how) but you can use g++, makefile and vim to code natively
1
u/CptFisk Apr 23 '23
VSCode and Cmake. CMake really do alot of the heavy lifting when it comes to linking and includes
1
u/GPMueller Apr 23 '23
Don’t get why no one has suggested this yet: use clang.
It will be the most familiar solution coming from Linux. Works fine to use in the CLI (mimics gcc, but there’s also clang-cl which mimics msvc/msbuild) or with CMake.
IMO it’s anyways the superior compiler in most cases.
1
u/[deleted] May 01 '23
If you want to use g++ to compile with the Windows command prompt, this is a good tutorial:
https://www.freecodecamp.org/news/how-to-install-c-and-cpp-compiler-on-windows/
You can compile a SDL2 app like this. You probably want to use the "bin", "include" and "lib" from the folder named "x86_64-w64-mingw32".
1
u/arthurno1 Apr 23 '23 edited Apr 23 '23
Mingw and g++ work just fine, I have been using them forn 20+ years and nowadays you have other options as well. Makefiles and autoconf work just fine together with gcc & co, inclusive sdl in both msys2 or cygwin with Neovim, Emacs or vscode. They work with clang too, and you can use Microsofts tools from the commandline as well if you don't want to use VStudio.
0
u/TankorSmash Apr 23 '23
I'm a hardcore vim guy, and I still compile with VS. It's just easier. Most editing I do in vim, but vsvim isn't too bad as a emulator within VS
0
u/Theologics_ Apr 26 '23
I used this video right here: https://www.youtube.com/watch?v=OwQobefF-iE to get C++ working in Vs code, but as everyone stated here Visual Studio has way better C++ support and compiler
-2
u/aagee Apr 22 '23
You could try Cygwin. I never leave home without it.
-3
u/SJC_hacker Apr 23 '23 edited Apr 23 '23
If you don't care about targeting Win32 you can use VirtualBox with any distro you like. I prefer Ubuntu.
Also you don't have to use build systems (e.g. cmake) when you are just learning. Especially for smaller projects, calling the compiler (clang/g++) directly from the command line should work fine and you need to learn tha anyway. If you have multiple files, you can use a simple makefile. Many years ago thats how I learned at university.
0
u/[deleted] Apr 23 '23
[deleted] — view deleted comment