c++ including cpp files - c++

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!

Related

How to compile multiple standalone C++ program in a single Visual Studio Code Solution

I want to compile single standalone C++ programs in Visual Studio Code. Currently, when I compile a program in VS Code, it only outputs a single executable file. If I have one program helloworld.cpp, it will only compile and create a helloworld.exe in the folder.
If I want to create another program, for example, Test1.cpp the executable should be named Test1.exe in the same folder and so on.
Your problem is not with the IDE - .cpp or .c files are not "program", they are translation units. Compiler creates an object file from each translation unit, and than linker create a "program" if it can find a spectial function - main.
So, if you want to compile multiple program, you should create a separate project within your solution. I guess, in MSVS Code you can add a new project to existing solution. If you use CMake this equivalent to add add_executable for each program.

CPP codes linking in VScode with Mingw-w64 in Windows 10

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.

g++ ignoring header files

So I am trying to create a exec with g++. My command is as follows:
g++ -o project21 main.cpp tools.hpp file.hpp FileInfo.cpp file.cp tools.cpp
I get an error
ld: warning: ignoring file tools.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
ld: warning: ignoring file file.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
The behavior here is that odd is that I can compile it in XCode and using the exec just fine it is just in the shell that I am having the problem.
You don't need to compile the hpp files. Include them in your .cpp files.
Even if you include the hpp on your project, Xcode won't compile them, unless you explicitly ask it to (my mac is at work, but if I recall click the project (root of the tree at the left), click a target and check the tab build phases, there is a list with the files to compile).
About the segmentation fault, if it were missing source files (i.e, you added code in the .hpp files that really should be on a .cpp file), then you would get a link error, and not a segmentation fault. There are other reasons to the segmentation fault, run on GDB and debug it (compile with -g to be able to debug). Here are some possibilities:
If you are building for a different platform, the problem may be some platform specific code.
Compiler incompatibility: assuming you were using Xcode with LLVM, you may have some code that works with LLVM but behave different with gcc. Very unlikely (by my experience with those compilers, I can say they are very compatible), but still possible.
Try deleting any *.gch files that may have been created by the Xcode build before trying your g++ command.
However, in general I'd try to get things working without pre-compiling the header files first, then add the pre-compiled header optimization if it's necessary (i.e., don't pass the .hpp files to the compiler explicitly until you've got everything else working and only if you really need to).

How to compile/use intellisense on miscellaneous files in MSVC

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.

How to output preprocessed code AND compile it (Visual Studio)

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