I'm generating preprocessor output (.i) from Visual Studio, but also want to do the actual build. Is there a combination of flags that will both output the .i file without then stopping the compiler from going ahead with the build as normal?
This is currently just C++ but will probably want to use this with CUDA later, so prefer answers that work within Visual Studio rather than require command line (unless it works for CUDA too).
The point of this is to save the time it takes to do Project->Properties->Config Props->C/C++->Preprocessor->Generate Preprocessed File Yes/No plus Rebuild. Particularly it is irksome to me that the preprocessor has to run twice, so a solution that somehow generates the .i file in part 1 and then compiles that file in part 2 would be fine.
The exact version of Visual Studio I'm using is VS 2008 Express
You can add a custom build step or a new target to dump the preprocess after the code is built by definition the dumped preprocessor output is what is built.
You can create a custom build config that does pre-processing and then define a batch build that builds the pre-processed version followed by the actual compiled / linked version.
Use the switch /P Example- cl /C sample.c
The above will generate a .I file with same name (sample.I). Now to compile the .I file, just rename .I file to .C then do cl /c to compile and generate an object file.
If your running GNU gcc/g++ compiler, then configure a Makefile target "recipe" with the following code
gcc -save-temps -dumpbase save-foo -c foo.c
Reference can be found in the terminal man pages for gcc.
man gcc
section:
-dumpbase dumpbase
This will create a separate .i file and compile "foo" to object code. That code can then be used as a prerequisite for your target "goal" executable. Link below explains the vocabulary I used.
https://www.gnu.org/software/make/manual/html_node/Simple-Makefile.html#Simple-Makefile
Related
I'm learning C++ with VScode with Mingw-w64 in Windows 10.
Today, I watched how to make header files and build the separated codes in Visual Studio 2019.
I wanted to implement this process in Visual Studio Code editor, but I met linking error.
My naive solution is like
~$ g++ -c .\main.cpp .\add.cpp
~$ g++ .\main.o .\add.o -o test.exe
First, compiling the source codes into object files.
Second, linking those object files to an execution file.
It works anyway, but when I tried to run the debugging mode, it doesn't work.
I think I need to edit launch.json file, but I have no idea...
The following images are about my situation.
How can I build those separated codes at once?
In this time, I just described the path of the definition code into 'tasks.json' file.
After then, built the main code again, so that it worked.
I am a complete programming (and generally IT) noob and I am learning C++ using Visual Studio environment. I would like to explore the compiler, and see my temporary files (ending with .ii, .s and .o).
My textbooks tell me to instruct the compiler to save using the -save-temps option. What I don't understand is how do I actually access this -save-temps option? Is there some toggle option I click in Visual Studio to do this? Where do I find it? Please bear in mind I am completely new to the Visual Studio environment.
You should use g++ (GNU C++ Compiler) to compile your source code and not Visual Studio.
To get .o files run g++ main.c -o main.o. This will give you the object file.
It is incomprehensible as it is a binary file not a readable text file.
To get .s files run g++ -S main.c -o main.s. This will give you the assembly file.
This file is readable as it is the assembly of your source code.
when I work on my IDE (visual studio or xCode) I never include cpp files (only h files) and everything compiles perfectly.
however, when I compile on the cmd/terminal (mostly on my raspberry pi) if I don't include the cpp files the compiler throws many errors because he didn't read the cpp files.
why is that? how does the ide knows to include the currect cpp files? and is there a way to find out if you are compiling through an ide or terminal?
something like:
#ifdef IDE_KEYWORD
#endif
The IDE will typically compile each source file (what you are describing as a "cpp file") separately, to produce an object file for each, and then link all the object files (and other libraries). Mechanically, the IDE does that by invoking the compiler separately for each source file listed in the "project file" and then linking.
When compiling from the command line, it is necessary to specify EVERY source file on the command line - the IDE isn't helping with that, so you need to do it manually.
One option is to compile every source file separately (specify each on a separate command line) and then another command to link all the object files together to produce the executable. In effect, this (more or less) imitates what an IDE does.
A second option is to list EVERY source file on a single command line - which will cause the compiler to compile each one sequentially and then (depending on what command line options you specify) link the object files together.
IDEs track what files are in the project/workspace and what ones are not. For instance, Visual Studio has a block in the vcxproj file which stores all the compilation units as such
<ClCompile Include="yourFile.cpp" />
having an ifdef to determine if you are using an IDE should be just a matter of adding IDE_KEYWORD to your preprocessor definitions inside your IDE. then if you compile outside of that IDE, the IDE_KEYWORD wont be defined.
If you are in Visual Studio you can
#ifdef _MSC_VER
// your code
#endif
If not I suppose there is no way.
The error may occur because you are not compiling all the .cpp files and/or are linking not all object files.
If using g++ g++ -c file.cpp -o file.o for every file to compile them. Then g++ file1.o file2.o fileN.o -o finalExecutable.exe for linking.
If this seems complicated look into Makefiles. They are awesome!
I wanted to convert my simulation code in Matlab to C++ and run it with a C++ compiler such as Visual Studio or g++. I modified my code to successfully build a C++ project using Matlab Coder.
The coder returns a .prj file in the top project directory, and tens or hundreds of c++ source/header/object files in the dll directory. I tried to open the .prj files with Visual Studio 2012 or 2013 (I tried with both versions) that was unsuccessful and they cannot even open a .prj file.
I have made sure to configure the Visual Studio as the C++ language compiler in Matlab using mex -setup.
Does anyone have any idea how to handle the .prj and the subsequent files with Visual Studio or g++? I don't want to make it executable at the moment; I just want to run it with either compilers and make sure it's working.
Thanks
The prj file that is generated is used for the MATLAB Coder GUI, rather than Visual Studio. If you double-click that file in MATLAB, the Coder GUI will open and you can generate code using it.
For a simple example of using the generated code in Visual Studio you can refer to the documentation.
If you are going to use the generated code on the same platform on which you are running MATLAB, I would recommend changing the output type to a static library (LIB) or a shared library (DLL). Doing so will cause the C or C++ code to be generated and then compiled into a library using the compiler that you specified. Then you can write code that calls the functions declared in the generated header file(s) and link in the generated library when compiling your hand-written code.
For Visual Studio, you'll need to add the codegen directory, codegen/lib/<proj_name> to the include path and add the generated LIB/DLL file as a dependency. With GCC, you'll need to add the same directory to the include path when compiling your code and link in the library.
The second half of my other answer has more information in it.
Alternatively, if you have a C main function written, you can set the output type to EXE and specify the main function to have MATLAB Coder generate code, compile it with your provided main and compile a standalone executable.
Does anyone have any idea how to handle the .prj
you supposed to run "mcc" on .prj file, I believe
Quote
"Specify that mcc use settings contained in the specified project file. Use
-F project_name.prj
to specify project_name as the project file name when calling mcc. This option enables the .prj file, along with all of its associated settings, to be fed back to mcc. Project files created using either mcc or deploytool are eligible to use this option. When using -F, no other arguments may be invoked against mcc."
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