How to add certain commands when calling the compiler in VS Code? - c++

I have changed my IDE's from DEV-C++ to Visual Studio code, but now, I cannot add my opengl addons when compiling the program. DEV-C++ gives you the posibilty to add addons to the compiler call, for example I used -lglu32 -lglut32 -lopengl32 for my opengl programms making the compiler call look something like this: gcc.exe "C:\Users\Main\Downloads\C\OpenGL Base\main.c" -o "C:\Users\Main\Downloads\C\OpenGL Base\main.exe" -lglu32 -lglut32 -lopengl32 -std=c99 -I"C:\MinGW\include" -L"C:\MinGW\lib" -static-libstdc++ -static-libgcc however I do not know how to recreate this automatically in VS Code without having to rename the address for every different program I compile.

First, visual studio code is just a code editor with some extra features like an embedded command line terminal or extension that can be installed, providing extra functionality. This means that there are many ways to build a C/C++ application, e.g. by writing your own scripts that you run via the terminal, or by using build systems like Make or others via terminal or an installed extension. You probably want to take a look at the C/C++ extensions available for visual studio code. There are already other stack overflow articles already covering this topic:
How do I set up Visual Studio Code to compile C++ code?
If you just want to use the same flags for building different applications, you might want to just put the command into a script which uses the current directory (if this is what you meant by address). That way the script would work for different projects/applications located in different directories.

Related

Simplifying g++ command on Windows

I have been toying around with javidx9's olcPixelGameEngine (a single-header game prototyping engine) for a while now, but I have been experiencing some problems related to the necessary libraries for compiling the code. The header file offers many #pragma comment for telling Visual Studio what libraries to link, but since I can't use Visual Studio (my computer is very old and can't run it well), I tried compiling by hand, linking each library. The resulting command ended up being something like g++ .\main.cpp -std=c++17 -luser32 -lgdi32 -lopengl32 -lgdiplus -lShlwapi -lstdc++fs, which is a tad too big for my liking. Is there a way to simplify this entire command into something reusable for other .cpp files? I thought of make files, but since I use mostly Windows, I don't know how to use them. Being able to link automatically, or just mixing all those flags into a single command would be very helpful.
I'm currently using MinGW, installed with MSYS2, for compiling, if that can help.

How to compile a visual studio c++ project on linux efficiently

I'm having a C++ Project in Visual Studio 2017 which uses curl. It compiles fine on a linux machine. But i think my procedure is not efficient. Im just copying my source files to the linux machine and run
g++ one.cpp two.cpp etc.cpp -lcurl
or even
g++ *.cpp -lcurl -o output
Is there something to do this a "cool" way? Like cmake or something (sorry im not into this). An Example would be really helpful
Your options range wildly from:
Just keep doing what you're doing
Wrap that command in a shell script so you don't have to keep typing it
Make a Makefile to auto-generate that command
Use CMake to auto-generate the Makefile (or automake or something else)
Use an IDE (examples for various platforms: Eclipse, Xcode, Visual Studio) to fully manage the project for you, including build rules/commands — Visual Studio Code in particular may be of interest here
It is completely up to you what you pick.
Personally, in your situation, right now I'd just throw together a Makefile and be done with it, until your needs become more complex.
Whichever of the latter 3 options you pick, there are abundant examples online and in your book already.
I would say, please don't use a really complicated solution to solve a simple problem just because it's "cool", because that's not what cool is.

Getting the C++ compilers in Code::Blocks and Visual Studio to adhere to C++(11) standard?

I use two C++ compilers / IDEs. (1) g++ in Code::Blocks and (2) Visual C++ in Visual Studio.
I have some question on how to set up the compiler in the way I want it to be.
Code::Blocks: I want to make G++ follow standard C++. For example:
int x;
cin >> x;
int arr[x];
...wouldn't work in standard C++.
In standard C++, one has to use dynamic allocation. For example, the Visual C++ compiler would reject this code. However, this code would work in Code::Blocks.
How do I make g++ reject this code?
Also, how do I tell g++ and Visual C++ 2013 to follow and only follow C++11 standard? It seems to me the default one is still C++98/03.
Compiler options for standard-conformance.
With g++ you want, at minimum,
-std=c++11 for the C++11 standard.
-pedantic to warn about use of language extensions.
-Wall -Wextra to up the warning level to reasonable.
With Visual C++ you want, at minimum,
/nologo to turn off a verbose version and copyright message,
/EHsc to turn on exception support,
/GR to turn on RTTI support, Run Time Type Information such as dynamic_cast,
/W4 to up the warning level to reasonable.
With Visual C++ there is no special option to indicate the C++ standard version.
How to preset options for command line use.
MinGW g++ generally (one exception is the Nuwen distribution) defaults to producing executables that rely on DLLs that reside in the mingw\bin directory, which means that you generally need that in your PATH environment variable.
This means that it's not such a good idea to add options via a batch file or shortcut or command intepreter macro. Instead use option -dumpspecs and redirection to create a specs file (with that name) in the directory specified by this Windows command:
g++ --print-search-dirs | find "install:"
Read up on how to edit that file, then fix it. ;-)
You might want to set the CPATH environment variable to your general include directory.
With Visual C++:
Compilers options can be preset in the CL environment variable.
Include paths in the INCLUDE variable.
Library paths in the LIB variable.
Linker options can be preset in the LINK variable.
For the linker options you may want to have /entry:mainCRTStartup to support ordinary standard main startup function also when producing a GUI subsystem executable. However this conflicts a little with MFC. It's been many years since I did anything MFC so if you want details or solution please post a separate question which others can answer.
How to set options in each IDE (Code::Blocks, Visual Studio).
This is as simple as reading each IDE's documentation. ;-)
Note that options can be set both globally and per project.

Compilation QT Creator project using classic terminal and gcc/g++

I'm tired searching in internet.
I created a klient-serwer program, which shut down computer just by their IP's on specific port.I made whole thing in C, everything works perfectly, i'm compiling my project using gcc with -Wall option and I've got a clear results.
But I had to create GUI so i decided to use QT Creator IDE, I transfered code from C into C++ and i made GUI. Program works but I have to compile this using g++ with -Wall option.
Thing is, I cannot compile anything.
g++ my_project.cpp -o mypro -Wall
ERROR: fatal error: QMainWindow: directory don't exist (in my native language)
compilation terminated
(same with other source files)
Any ideas?
It's propably very easy, but believe me, I want to compile this and go to sleep. Cheers
Why don't you use qmake? First, you can edit your *.pro file and add any compiler flags you wish (see Mitch's comment). Then, if you execute qmake and then make, it will show you a sequence of compilation commands with proper flags, linker and include paths and so on. After that you can just reproduce that commands manually if your teacher wants so.
The error shows that the compiler is trying to open QMainWindow as if it was a directory (if the translation is correct) or at least failing to locate where the include files for Qt are. You probably need to provide some -I option to hint the compiler as to where the Qt headers are, and make sure that the #include directives are correct in your code.

Compiling dlib examples on Windows?

I'm fairly new to C++ (a long time Lisp programmer) and am trying to compile some of the examples for dlib on Windows using MinGW. I added dlib into the PATH. I then call g++ timer_ex.cpp from the examples directory. But I get a lot of error messages.
Short of using Visual Studio, what's the best way of compiling dlib examples on Windows?
Adding the folder to PATH usually doesn't work out well for me. Instead, try this command. I just compiled the example with it without error:
g++ timer_ex.cpp ..\dlib\all\source.cpp -I.. -luser32 -lws2_32 -lgdi32 -lcomctl32 -limm32
The somewhat cryptic -I.. adds the folder one level up to the include search path. This is the right thing to do assuming your haven't changed the folder layout. But in general this is the easiest way to add something to the compiler's include search path.
You also probably want to add the -O3 option which will tell gcc to produce optimized executables. Generally this makes the resulting application a lot faster, especially if you are doing heavy numerical work.
As an aside, you should consider installing CMake. It's a convenient tool which sets up a project like this for you. It works on Windows, Linux, Mac OS and many other platforms. To use it to compile the dlib example programs you would just have to say cmake . from within the example folder and then make. There is also a free version of visual studio which is quite nice, and as a bonus cmake can automatically create the project files for you.