How to compile/use intellisense on miscellaneous files in MSVC - c++

When I open a .cpp file separate of any project in MSVC, the debugging tools are greyed out and intellisense doesn't seem to be parsing the text. Is there a way to make it so when I open an individual .cpp file, I can compile it straight away without having to copy paste the code into a new project?

the debugging tools are greyed out and intellisense doesn't seem to be parsing the text. Is there a way to make it so when I open an individual .cpp file
You can't debug it, because there is no program. One file does not make a program. Project "makes" a program.
Is there a way to make it so when I open an individual .cpp file, I can compile it straight away without having to copy paste the code into a new project?
This won't work, because to compile even one file you might need specific compiler settings (include directories), which are specified within project.
However, you can use "Visual Studio command prompt", locate your file and run cl filename.cpp (filename.cpp is your file) command which will attempt to compile/link single file without specifying extra settings. You won't get assistance from ide in this case, but you might be able to compile it (assuming the file contains trivial program that doesn't need extra settings) and read any errors you get from command prompt.

No, there isn't. The minimum unit Visual Studio can build is a project.

Related

How do I run .cpp files in Visual Studio?

I have been following tutorials which have me download and unzip projects which contain .sln files for me to open. Following them this way is pretty easy. However, I want to be able to download a single .cpp file and run it without creating a project. I just want to get straight to into it. In Code::Blocks, setting this up is easy to figure out. For some reason, I can figure it out in Visual Studio.
I want to be able to download a single .cpp file and run it without creating a project.
You cannot. Visual Studio does not support this. A project is always required, even if it only contains a single source code (.cpp) file.
You can, however, run a single .cpp source file through Microsoft's C++ compiler on the command line (cl.exe), and then execute it. But this doesn't involve anything about the Visual Studio IDE.
if you have the source code, you could make a .cpp file by right-clicking and adding one and then ctrl+s then f5 and it might run... Idk.

How do I debug existing C++ source code in Visual Studio 2015?

This may have a very simple solution, but being new to Visual Studio and C++ programming, I'm having a hard time with this.
I downloaded an SDK written in C++ which contains an executable file and also the source and header files. The executable file accepts some command line arguments. So far I've been running the executable file from the windows command prompt (like C:\path\filename.exe -argument), but now I want to be able to enter these command line arguments and then place breakpoints in the source code for debugging the source code.
I don't know how I can open the source files in Visual Studio and debug it. If I just open the source file with the main function, the debug button says 'Attach' on it instead of debug.
I see another similar question here, but that question is for a project developed using Visual Studio whereas the source code I have does not have any Visual Studio project/solution files. The only files I have are the executable, the source and header files (.cpp, .h, .hpp), and CMakeLists.txt files.
You can "open" the exe as a project (you can achieve the same if you drag and drop the exe into VS icon). Then you can add command line parameters at Project Properties. You will need to have debug symbol information (usually a .pdb file), if you want source code level debugging (values of variables, etc.). If you don't have that, you can only debug at the disassembly level.
You may want to create a proper project for the source files - it is an easy task, if the project is simple - so you can rebuild the exe.

Visual Studio custom build-generated file included by a .cpp

I have a custom build rule on a data file which generates a header file. The custom build rule works fine (runs when the data is changed and does not run when it is not, as I would expect it to).
However, this generated header file is included by a .cpp file which is compiled using the default C++ compiler. When the header is generated and there are no changes in the .cpp itself, the build system will not compile it. I have to build twice, once to generate the header file and then once more for the build system to notice and rebuild the .cpp as well (the generated header has the current date/time so there are no problems there, it just seems that Visual Studio makes all the checks before starting the build).
Not a major issue but it gets annoying after a while. It is also error-prone. Is there a way to either expedite the custom build rule so that it would run before Visual Studio checks the dependences of the .cpp files or maybe to run the build twice? There may be several files using this custom build rule so using a pre-build step to build them all would get messy.
I'm using VS 2008 if that makes any difference. The custom build rule has the "Outputs" field correctly filled in so it has the information about the files the custom build generates.

Compile a specific obj in a Visual Studio Project from the Command Line?

I want to compile file.obj from the commandline. Within the IDE, if I'm viewing file.cpp, I can click on Build -> Compile (or just hit Ctrl-F7), and it will compile just the file.obj object. I would like to be able to do this from the commandline. Ideally, something akin to:
vcbuild project.vcproj Debug file.obj // not a valid command
I have looked at the documentation for vcbuild, msbuild, and devenv. I've also experimented with all three, but I cannot find a way to do this. I can find a way to build an entire project, but that's not what I want. I want to build a specific source file. /pass1 tells vcbuild to just compile (not link), but it compiles the entire project.
I also looked at using cl, but that is just the compiler. In order to use it, I would have to know all the right parameters to pass to set up my environment correctly. All that is automatically taken care of with msbuild/vcbuild.
With Makefiles, I could always do make file.obj, and it would properly set path, include dirs, etc.
Any options for this? Is there an automated way to extract the appropriate settings from the .vcproj file, and pass them to cl?
Using cl is the way to compile single files from the command line. Like you say, it requires/allows you to specify exactly the options you want to use. All the options!
If you actually don't want to do that, why not use the IDE to have it done automagically for you? Why do it the hardest way, if you don't like that?
if you just want to compile the project, run the visual studio command line and call msbuild.
Example:
MSBuild.exe MyProj.proj /property:Configuration=Debug
this will compile the MyProj Project from the current directory.
more info on msbuild
http://msdn.microsoft.com/en-us/library/dd393574.aspx
Or if you need to build a single file you can use cl as stated above. You can see all the parameters passed by visual studio to cl if you go in the properties of the project. Usually under:
Configuration Properties -> C/C++ -> Command Line
and for linking:
Configuration Properties -> Linker -> Command Line

Can the list of C++ files in a Visual Studio project be dynamically filled?

I have a tool that generates most (but not all) files that need to be compiled in Visual Studio. The tool reads a configuration file and generates c++ files afterwards. This list can differ from one call to another when the configuration is altered.
I am wondering whether it would be possible to adapt the compiling process to my needs, which would be :
Launch the tool (not needed if configuration file has been modified)
Retrieve new list of C++ files to be compiled (ideally isolated in a folder inside the project)
Compile C++ files
EDIT: Having to close Visual Studio for this process to work is a no-go for me. The idea would be to have cpp files dynamically added as the first step of the compilation process.
Use a pre-build step to run your tool.
Also, create a file containing the list of includes and sources
This file name should be fixed (so that you don't have to change project properties or the vcproj file) -- add it to the project. E.g:
Project Properties > Command Line > Additional Options > #headerListingFile
You are not trying to integrate lex/yacc output with VS, are you?
Would CMake help? It's an automated project manager that generates Makefiles and VS projects for projects you define. Just add a source file, re-run CMake and you're done.
I think what you should do is create a custom makefile and use that for builds.
Please see this page for more information.