How are arguments passed into custom build tools for Visual Studio? - c++

When Visual Studio uses a custom build tool, how is the file being built and command line passed into the .exe file?
I'm assuming that the file and command line is passed into the files main function, but I do not have much else to go on. Any thoughts?
Edit: I want to create a build tool for processing custom files therefore I need to know how Visual Studio sends arguments and data to the executables.

Visual Studio simply runs your executable with the arguments you provided just like any other application. If you create a build tool that will be called by Visual Studio like this
my_build_tool.exe /foobar C:\my\path\to\file.cpp
then you will access it in main function of your program in standard way:
int main(int argc, char *argv[])
{
// arg[1] points to "/foobar" and arg[2] points to "C:\my\path\to\file.cpp"
...
}

Related

How can I create a c++ header file in Visual Studio Code?

I have a program with 3 files, and when IrRun the program via Code Runner it keeps printing errors.The icon for the "Log.hpp" file is C, not C++. It doesnt matter if I rename it to Log.h or anything, it seems that I cant create a c++ header file in vscode.
The 3 files are:
main.cpp
#include <iostream>
#include "log.hpp"
using namespace std;
int main() {
InitLog();
Log("Hello World");
return 0;
}
log.cpp
#include "log.hpp"
#include <iostream>
void InitLog() {
Log("Initializing Log");
}
void Log(const char *message) {
std::cout << message << std::endl;
}
log.hpp
#pragma once
void InitLog();
void Log(const char *message);
The error mesages are:
main.cpp: In function 'int main()':
main.cpp:5:5: error: 'InitLog' was not declared in this scope
InitLog();
^~~~~~~
main.cpp:6:5: error: 'Log' was not declared in this scope
Log("Hello World");
Need help.
Are you using Windows? Which compiler? Do you have Microsoft Build Tools installed or even Visual Studio? Are you using gcc or clang?
You must start VS Code from a developer prompt: Open a developer prompt console, navigate to the folder where you code is. Then enter
code .
Visual Studo Code will then open with the environment set up. Then open a terminal inside VSCode using Control-` and try
cl /EHsc main.cpp log.cpp
And it will create main.exe. You can run it in the terminal and it will not close... Next time you open the project you can just open the folder in VS Code, since it would then already have the json config files created
VS Code is just an IDE. So compilers must be installed and also extensions. And some JSON files and tasks must be set up. It is sometimes simple, sometimes not so simple, I believe. But doing that you will then have a powerful editor and an unbelievably flexible environment, since you can for instance run and debug code in nodejs or C or C++ or anything and even in Linux without leaving the session on your casual Windows machine.
Your question is about a specific vscode plugin called Code Runner. It's not really related to vscode. The way how Code Runner is designed will never work for multiple C++ source files. It's not the right tool for the job.
Perhaps you'd better off with Microsofts CMake Tools package for vscode. It does require you to create your own CMakeLists.txt file though. In your case you'd need nothing more than:
add_executable(log
main.cpp
log.cpp
)

Visual studio 2015 openmp support

I'm having trouble getting OpenMP support in Visual Studio 2015.
I've configured the project options to use /openmp (project->properties->C/C++->language->OpenMP support:yes), the code is as follows (very simple code, to test OpenMP):
#include <iostream>
#include <omp.h>
int main(int argc, char* argv[])
{
int n = 0;
#pragma omp parallel
{
std::cout << std::endl << "Hello World!";
}
return 0;
}
Only one thread runs and "Hello World!" is printed only once.
I was able to compile the program with VS2015 Community Version 14.0 Update 1 on Windows 8.1 64bit with OpenMP support.
Below, follow a list of steps that may help:
After create a new project and paste the source code, go to
Project-> Properties -> C/C++ -> Language
Change Open MP Support to Yes(/openmp)
Click Apply
On the left menu, go to Command Line and confirm that /openmp
appears somewhere at the compiler's options.
If it appears, click Ok and build the project.
Before run the program, put a breakpoint at the line:
int n = 0;
Run the the program by clicking on Local Windows Debugger
When the program stops at the breakpoint, go to Debug -> Windows -> Disassembly
Somewhere, near the breakpoint, look for an assembly line like:
call __vcomp_fork (?????????h)
If you find this line, chances are that openmp is ok and running.
Some other checks that can help:
Get a tool from Windows Sysinternals like Process Explorer (GUI) or ListDLLs (command line).
ListDLLs:
With the program stopped at the breakpoint, open task manager and look for the PID of the process.
Open a command prompt and run the command:
listdlls [PID] | findstr -i vcomp
Should appear something like VCOMP140D.DLL or VCOMP140.DLL or VCOMP????.DLL.
If it not appears, probably the compiler couldn't find the openmp dll so you'll have to see if this library is available at some point on your system.
Two last tips that may save your time:
If you change any configuration (e.g. Debug -> Release or x86 -> x64),
check again if Command Line has the /openmp option set ok.
If you try to force the compiler to C language (instead of C++), maybe the pragma:
#pragma omp parallel for
will not work (Update: Apparently this problem does not happen anymore on VS2017).
It shows me the message:
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe'
Come back the compiler to C++ language and the parallel for will works ok.
Partial answer: I was not able to get the compiler to accept /openmp through the config/GUI, but compiling in console with cl.exe /openmp works.

How to compile c++ file in visual studio?

I am new to Visual Studio and I don't know how to compile a .cpp file. I made just a single .cpp file (ctr + n => Visual C++ => C++ file) and I tried to compile it. But in place where normally there's a compile button (like with c#) there is strange 'Attach' button. I don't get what's going on, but I thought, Visual C++ might be some different version of normal C++. If so is that possible to compile normal C++ file in Visual Studio?
The problem is, Visual Studio don't really know, what to do with your .cpp file. Is it a program? Try the following:
File | New project
Visual C++ | Win32 | Win32 Project
Select a name and location for the project
Next
Choose Console application
Choose Empty project
Deselect Precompiled header
(optionally) Deselect SDL checks
Finish
Right-click on Source files and choose Add | New Item...
Choose C++ File
Choose name for this file
Write the following inside:
#include <stdio.h>
int main(int argc, char * argv[])
{
printf("Hello, world!\n");
return 0;
}
Press F5
You should, just as you did for C#, create a C++ project and add your source file to that. Then there will be all the build options you ever dreamed of.

Building and running C++ console application with standard 'main' entry in VS (2010)

I am using Visual Studio 2010 and started console application project.
VS generates the entry point as _tmain(int argc, wchar_t *argv[]). Instead, I need the entry point main(int argc, char *argv[]) - just a standard console application.
What are the settings that I need to change from defaults and how does it work?
Simply create an empty console project (there's a setting when you pick the project type), then add a "main.cpp" file and add the standard main function. Works every time for me. Actually, I never use the premade/generated C++ projects due to overhead, precompiled headers, etc.
When you create a new project, mark the "empty project" box.
Then you can write any main you want.

Building a C++ project in Visual Studio doesn't create any files

I recently decided to start learning Visual Studio so that it replaces my need for CodeBlocks and MinGW for C++ programming.
So, today I made a new Win32 C++ Console Application, wrote down this code in a new .cpp file
#include <iostream>
using namespace std;
int main()
{
int a;
cin >> a;
cout << a << endl;
return 0;
}
and compiled it. The log said
1>------ Build started: Project: CPP_CONSOLE_TEST, Configuration: Debug Win32 ------
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppBuild.targets(357,5): warning MSB8004: Output Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Output Directory.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
and I though my code was compiled and my .exe was created.
Then, upon trying to debug my program, Visual Studio said:
Unable to start program 'C:\Users\XYZ\Documents\Visual Studio 2013\Projects\CPP_CONSOLE_TEST\Debug\CPP_CONSOLE_TEST.exe'. The system cannot find the file specified.
I then opened the Debug folder of the project and it was completely empty...
I've been searching around Google for some time and I even tried to "Repair" my Visual Studio build with no results. Any help?
Quick edit: Just tried compiling a C# app, just to see if the IDE itself was the problem. It compiled and ran just fine, so it's some issue with the Visual C++ compiler and its settings...
Turns out I hadn't added the source file to the Project... :|
Visual Studio, has its own vision of c++ projects. By default, it needs a #include "stdafx.h" on top of your cpp file, with the associated stdafx.h and stdafx.cpp files.
Then, in a c++ visual studio project, the real definition of the main function is int _tmain(int argc, _TCHAR* argv[]). But it should work with your definition.
Why don't you try to use Serge Rogatch's solution?
There is a bug in Visual Studio which leads to problems when project has long path.