1

I've recently started learning C++, and I've downloaded Microsoft Visual Studio to run my programs. To practice, I want to go through the Project Euler problems. As it is, I'm making a different project for each problem, which seems excessive to me.

Another option would be to have in the main file

#include "Problem[x].h"
int main(){
    solve[x]();
    return 0;
}

where I would manually replace [x] with the problem number (e.g. "Problem2.h" and "solve2"), and then have a header file (e.g. "Problem2.h") with

void solve[x]();

and the C++ file (e.g. "Problem2.cpp") would have

#include <iostream>
#include "Problem[x].h"

void solve[x](){
    //stuff
    std::cout << answer;
}

The problem with this approach is that for every problem, I'd have to make two new files (header and C++). Is there any way to avoid making so many new files? If I could have a different C++ file for each problem, and then "call" a specific one from a main file, that would be ideal.

  • I create a directory say `cpp` (put it wherever you want, `Documents` is fine). VS comes with a fantastic tool called the *VS Developers Command Prompt* that configures the build environment for use at the command line. (you can use `cmd.exe` or `PowerShell` as your terminal) I like `cmd.exe`. Put all your source files under `cpp` and now change to that directory. You can build any program you want using the compiler `cl.exe` (`cl /?` for all options). To build `file.cpp` into `file.exe`, simply use `cl /nologo /W3 /Ox /EHsc /Fefile.exe /Tp file.cpp`. No project, no muss no fuss. – David C. Rankin Nov 25 '21 at 07:09
  • If you create a `bin` directory under `cpp` you can put all your executables in `bin` using the `/Fe` option (above the change is `/Febin/file.exe`. This keeps your source directory clean of executables. You can do the same for `obj` files. Create the `obj` directory and add the option `/Foobj/file`. Now your excutables end up under `bid` and your object files under `obj`. You can write a short batch file to build for you (say `c+.bat`) where the file contains (among other checks) `cl /nologo /W3 /Ox /EHsc /Foobj/%~n1 /Febin/%~n1 /Tp %~1` Now it's just `c+ file.cpp` and it does the rest. – David C. Rankin Nov 25 '21 at 07:14
  • Don't forget to configure the terminal font and size to liking. Say 12pt and 60 rows by 116 cols works (adjust as needed) Also, set the buffer size to say 1000 lines so you can have a decent amount of output available and be able to scroll up to see it. A fringe-benefit of compiling from the command line is it forces you to learn what options you need to compile your programs. --- Then you can teach the IDE how you want your programs compiled (and not just pray VS set the defaults right...) – David C. Rankin Nov 25 '21 at 07:21
  • @DavidC.Rankin Would you like to make an answer? – Yunnosch Nov 25 '21 at 07:21
  • How about one complete cpp file (with `main()` etc.) for each solution and some macro shenanigans to make sure that only the content of one is built, selectable in a single header. (But I recommend to study Davids input first.) – Yunnosch Nov 25 '21 at 07:25
  • Yes, I can make it an answer. Give me a sec to write it up. – David C. Rankin Nov 25 '21 at 08:37

2 Answers2

1

While VS is a very capable IDE, one significant drawback is when you need to compile a number of simple files (even not so simple projects) and you don't want to spend the time setting up a new Project of each file you need to compile. The Answer Is - Compile from the command line, no project, no additional setup, just invoke the VS compiler cl.exe directly with the options needed.

VS provides just such a command-line called the VS Developers Command Prompt. VS provides the VS Developers Command Prompt with a complete build configuration for the command prompt. To build file.cpp into file.exe, simply use:

cl /nologo /W3 /Ox /EHsc /Fefile.exe /Tp file.cpp

(the /Tp is optional and just tells the compiler that the next source file is a C++ file instead of a /Tc (C file).

No project, no muss no fuss.allowing you to compile from the command line outside of the VS IDE. This allows you to compile any source file you like without setting up a project or messing with anything else.

Simply create a source directory to put all your C++ sources in, say Documents\cpp (it can be any directory you want). Now just save all your source files to that directory and open the VS Developers Command Prompt and change to that directory. The VS compiler is cl.exe. You can invoke it as simple cl and to see all options you would use cl /?.

If you create a bin directory under cpp you can put all your executables in bin using the /Fe option (above the change is /Febin/file.exe. This keeps your source directory clean of executables. You can do the same for obj files. Create the obj directory and add the option /Foobj/file. Now your excutables end up under bin and your object files under obj.

You can write a short batch file to build for you (say c+.bat) where the file contains (among other checks) cl /nologo /W3 /Ox /EHsc /Foobj/%~n1 /Febin/%~n1 /Tp %~1. Now to compile your code to executable, placing the executable in bin and the object file in obj all you need is c+ file.cpp and it does the rest.

A full example batch file that does basic checking that at least one arguments is given and that the file extension is .cpp could be written as:

@echo off

if "%~1" == "" goto insuffic
if not "%~x1" == ".cpp" goto notcfile

if not exist obj mkdir obj
if not exist bin mkdir bin

echo --
echo cl /nologo /W3 /wd4996 /Ox /EHsc /Foobj/%~n1 /Febin/%~n1 /Tp %~1
echo --

cl /nologo /W3 /wd4996 /Ox /EHsc /Foobj/%~n1 /Febin/%~n1 /Tp %~1

goto done

:insuffic
echo error: insufficient input.
goto usage

:notcfile
echo error: not a c++ file.

:usage
echo usage: %~nx0 file.cpp

:done

(minimal batch file to compile a source file as described above, with a few sane checks included. Just put this batch file in your source directory cpp)

The batch file above will create the bin and obj directories for you if they don't exits.

Being able to invoke the command line compiler will save you an incredible amount of time compiling short programs (with one or more source files). You can compile and debug 10 sources in the time it takes to set up and configure a single project in VS.

Not to mention using the command line will force you to learn what compiler options and libraries are needed to compile your programs. With that knowledge, you can then easily tell VS how you want your project configured, rather than just hoping VS set the correct defaults...

You can compile simple C++ files or full windows projects from the command line. It's all a matter of knowing what options are needed and what libraries need to be included to provide whatever type windows controls you are using.

Lastly, don't forget to configure the VS Developers Command Prompt with the font and number of rows/columns you want. Just click the icons at the top-left of the title-bar and choose "Properties". 12pt font works well with roughly 60 rows and 116 columns. (and set at least 1000 lines of scroll back so you can capture and review long output or at least as many as the lines of buffer you choose).

If you can't change the settings for the VS Developers Command Prompt directly, simply create a copy of the shortcut -- then you can save the font/rows/columns and lines of scrollback between sessions.

Give it a go. I've compiled hundreds, if not thousands, of programs this way and there is no more efficient way to do it. Let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I ran this line (```cl /nologo /W3 /Ox /EHsc /Fefile.exe /Tp file.cpp```) with a file that was just supposed to print out a test line. It made the .exe, but when I tried executing it, it runs properly (i.e. prints the line), but then immediately closes after running (I confirmed that it works by recording then going through the video). How can I keep it there until I close it? I'd prefer to do this without adding more to the code. – IX Nap Time Nov 25 '21 at 22:12
  • Add `getchar();` at the end to hold the terminal open until you press enter. Or *Just Run It From The Command Line...*, e.g. just type `file.exe`. No need to use the VS IDE for anything. (or if you put the executable in the separate `bin` directory, type `bin\file.exe`) I guess for all the old folks that started using computers before there was any type graphics interface, just running programs from the command line is natural. You really can do all you need to do from a programming or system administration standpoint from the command line alone.. – David C. Rankin Nov 25 '21 at 23:05
  • Ok, thank you so much for your help! – IX Nap Time Nov 27 '21 at 00:48
0

While I don't use Visual Studio, I am a C++ and Project Euler enthusiast. Although I do use the command line, I prefer compiling with the IDE (and CMake) and I do always create a new project for each problem, no matter how small. Beyond these preferences, however:

  • It's not strictly necessary to always have separate header and implementation files. The practice of putting only certain things in header files stems from C++'s compilation process and "one definition rule". But if you start writing template code, there might not be any implementation you could put in a separate file; part of the C++ standard library is header-only for this reason. It's explained in more detail in this answer. In short, writing all code for a solution in a single .h file (or .cpp!) and then including that file in main.cpp is equivalent to writing the same code at the top of main.cpp, and this will not be an issue if you're only including and compiling one such file at a time.

  • If any of this code is reusable and you wish to share it among multiple files, which is very good practice in general, then yes, it's also good practice in C++ to expose only as much as necessary in a header with "include guards", separate from its implementation. This can be just within a project, or you can separate the code into a library to share it across multiple projects.

sigma
  • 2,758
  • 1
  • 14
  • 18