How C/C++ headerfiles are processed in vscode - c++

I'm learning C now using vscode. I got curious and tried modifying the headerfiles. stdio.h to be specific and I found some really peculiar behavior. I studied the behavior using the simple Helloworld program.
infos: Windows 10 OS, VScode v1.56.0, Compiled using CL not gcc, ucrt file path:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt
The code:
#include<stdio.h>
int main()
{
printf("Hello world!");
}
So first I tried adding some random lines in the header file and save it. The code ran fine without any errors. I tried it after restarting. It still worked fine.
Then I commented the whole file with 2k+ and saved it and was expecting for the compiler to tell me that printf is not declared anywhere. But still again the code worked fine.
Then I reverted all changes, copied the header file to another folder and deleted the file in the ucrt folder
So. Now for the first time compiler said printf isn't declared anywhere.
I modified the headerfile outside and copied it to the urct folder and the code worked fine again.
So, as I checked into the file (in ucrt) the changes I made isn't showing here.
So I again deleted the headerfile in ucrt folder, cleared everyline added 2 3 random lines there saved it. And then pasted it in the ucrt to see that code still works really fine. And as I checked the header file.......It is the same old stdio.h that was when I installed it.
And Finally....when I copied the header file in ucrt and pasted it outside. And to my surprise It had the lines I added in No.5
There is only one explanation that I can think of...That VScode overrides any changes in the file.
So here are my questions:
How headerfiles are processed in vscode.
Can we actually modify it(The one in ucrt folder)
What is actually happening in the steps I mentioned above. Why these weird outcomes.
What is the difference between compiling with CL and with GCC.
NOTE: I know it's too lengthy I just wanted to give as much information I have.

VSCode itself doesn't do much with header files, the code explorer, intellisense and linter might have include paths but it is not responsible in any way for code-gen.
You can modify your system stdlib but I really wouldn't recommend it - my recommendation is to compile with -ffreestanding (on GCC and Clang) and then have a copy of a stdlib in your project and include it from there manually (Using -I or by using relative includes).
The most likely culprit is that your compiler & vscode's code explorer are being passed a flag which is modifying their include path, so the stdlib they are accessing is the system one, and the strange file modification behaviour can be attributed to how you are accessing the file (i.e. if you are using Go To Declaration).
Each may have a different default include setup, GCC uses the GNU Toolchain where it can - but ultimately they both follow the same preprocessing rules and its just a question of looking at what is included by default.
One last thing is that sometimes compilers will be nice and if stdio.h or the like isn't included and its a common or stdlib function, they will provide the correct prototype automatically and link as normal without the header being included - its not the case for my GCC config at least, and I'm not sure about CL

Related

Automake issue with linking

I'm working on a library that links another library to use its static functions.
Everything was working perfectly until I switched from including all my code in the header file to sharing it between the .h and .cpp file.
I tried to use ./configure --prefix="<same path as before>" and then make install but ended up with a "/usr/bin/install: cannot stat .libs/<library name>.so.0.0.0': No such file or directory" error. (also, I did change my makefile to accommodate for the inclusion of the .cpp file)
Then, trying to compile my old library (which should've still worked since the linked library didn't make install correctly and was still technically using the older version) I get a pile of errors that seem to have to do with c++ itself. For example:
.../gcc/4.9.1/linux64/include/c++/4.9.1/bits/stl_bvector.h:354:13: error: expected ';' at end of member declaration
There are an absolute ton of errors similar to the one above, all having to do with issues in the path .../gcc/4.9.1/linux64/include/c++/4.9.1/
Has anyone run into this error before? Let me know if I need to provide any more information, it's just that I've never had this happen so I'm at a loss as to where to proceed. It's likely something simple I'm missing but the longer I stare at it the less it makes sense.
Thanks!
Update: I ended up solving the issue I was having. Basically, the .so file was not being accessed, so I used make clean and then the linked library compiled properly. Next, the issues with the base library was just an extra character within a header file that was causing the compiler to blow up with over 1500 lines of errors. After fixing that, it was able to link up properly and is working fine now.

Need help understanding C++ libraries, compiling, linking, header files for specific project

This is my first stab at C++, also I know that the question is broad but I have a specific example that I'm working with so hopefully that will narrow everything down a bit.
I'm basically attempting to compile a C++ game manually in Linux (Ubuntu 14.04). The source code I am attempting to compile is located in this directory: https://github.com/akadmc/SmashBattle/tree/master/battle.
I'm CD'ing into the battle directory and, perhaps naively running
gcc *.cpp
I started seeing multiple issues as such:
compilation terminated.HealthPowerUp.cpp:1:21:
fatal error: SDL/SDL.h: No such file or directory #include "SDL/SDL.h"
and
compilation terminated.LaserBeamPowerUp.cpp:1:21:
fatal error: SDL/SDL.h: No such file or directory #include <SDL/SDL.h>
After researching header file includes I concluded that includes without <>'s are basically just relative paths to include a header file, and that when they are wrapped in <>'s they can either lookup the file through a listing of directories specified in an enviornment variable, or a command line option.
So my first question is, is there any reason the developer used
#include "SDL/SDL.h
AND
#include <SDL/SDL.h>
in different files? There was no SDL directory in the source code...
After realizing that SDL was missing from the source code / environment in one way or another I did tinkering. I was pretty confused (and still am) because I downloaded the SDL source files, didn't see any header files, ended up building a version of SDL by using cmake, and then build. I realized afterwards that I just made a local executable and didn't yield any header files. Then I realized that I just needed the development library, downloaded that, and put higher in the directory tree and then included it at compile with
c++ *.cpp -I $HOME/Desktop/smashProject/source/
Afterwards, the previous header file errors went away - but I started getting errors like the following:
Text.cpp:(.text+0x17): undefined reference to `SDL_RWFromFile'
Text.cpp:(.text+0x24): undefined reference to `SDL_LoadBMP_RW'
Text.cpp:(.text+0x34): undefined reference to `SDL_DisplayFormat'
And so on. Am I generally headed in the right path or do I have some misunderstanding about compiling, including development libraries, etc? Also I've read the the order of the compilation matters, and I'm not using any order + the developer didn't put a makefile in the source code or anything. I'm generally just confused as to how I should be doing this. Any help would be greatly appreciated.
Yes, you are on the right track. However, now you need to have a linkage to the SDL libraries. The -I just includes an extra library path but you have to actually link your assembly to the SDL files.
See this stack overflow question for more information.
How to compile an example SDL program written in C?

Codeblocks can't find header files

So a few hours ago I started learning c++ in codelite but I was getting frustated with, so I just got codeblocks and imported the project. But now whenever I try to compile it returns:
fatal error: imports.h: No such file or directory
This is my project hierarchy in codeblocks:
And this is what the project folder looks like:
What am I doing wrong?
I know this is years later, but I've recently seen students following what I deem is frankly bad advice such as what is given above. For those learning c++ this functionality is NOT for you. To add headers you should simply check that you are using double quotes, instead of angled brackets i.e.
#include "myheader.h"
and NOT
#include <myheader.h>
Angled brackets are meant for libraries (informally) and adding a simple header file for you basic classes does not require you to change default search directories. The problem comes when someone else tries to run your code (assuming you're doing this for uni) and their IDE isn't setup to search for a "library" (your header) where it shouldn't be. Double quotes tells the compiler the files exist in your current relative directory. This way you can keep your main, headers and header implementation in one directory. Fiddling with your IDE should only be done when necessary. KISS
You have to tell Codeblocks where to find the header files that you include. Try adding the full path to your '/Headers' in the include directories of codeblocks
Goto 'Codeblocks menu > Settings > Compiler > Search directories > Add'.
EDIT: Since your issue, however, is quite irrelevant to learning the C++ language itself, I suggest that you start with simpler programs, then move on to more complex ones. That, of course, unless you have previous experience with other programming languages
Since I haven't found any Makro for
#define 'hostname of device where compiler is located' // which is unique and not to be copied !
I have now successfully used and included
#include "myCompileEnv.h"
as a workaround with the comments above, which is located more central - above the project directories in CodeBlocks.

Cmake CHECK_INCLUDE_FILES not finding header file

I'm trying to have Cmake check if the file cxxabi.h is available. This file is from the c++ standard library, at least with g++. My current cmake commands look like this:
include(CheckIncludeFiles)
...
check_include_files(cxxabi.h HAVE_CXXABI)
if(HAVE_CXXABI)
...
else(HAVE_CXXABI)
...
endif(HAVE_CXXABI)
When this is executed, I get:
-- Looking for include files HAVE_CXXABI
-- Looking for include files HAVE_CXXABI - not found.
Although the file is available in /usr/include/c++/4.6.4/ and can properly be found by g++ when I compile a c++ code.
I suspect the macro check_include_files uses the C compiler instead of the C++ one to compile a small program that includes the required file, which of course fails since cxxabi.h is a C++ file.
Any idea how to solve that? (i.e. making the macro use the C++ compiler instead of the C one)
As edited in my original question:
Problem solved. There is a different macro for C++ headers, check_include_file_cxx, located in CheckIncludeFileCXX.
There exists another problem with CHECK_INCLUDE_FILES that I recently discovered with MinGW. The file tested was "ddk/ntapi.h". In the CMakeErr.log for this header I got a multiply messages like "DWORD - does not name a type" and so on for all MS types used in this header. Because of this reason the compilation fails and a requested header appears as "not found", whereas it is not true.
This happens because CheckIncludeFile.cxx contains only the requested header, and some headers in MinGW (and probably in the other APIs) does not include in its body all the list of required headers to be compiled in a standalone program that CMake creates.
The solution for this problem is to add absent basic includes into the CMAKE_REQURED_FLAGS, or as a third variable of CHECK_INCLUDE_FILE_CXX:
CHECK_INCLUDE_FILE_CXX("ddk/ntapi.h" VAR "-include windows.h")

Specify the name of compiled binary (*.exe) within source code in Visual Studio 2008

From this thread
http://www.codeguru.com/forum/showthread.php?p=1863547
It seems this cannot be done with:
#pragma comment(linker, "/out:mycool.exe")
Is there some simple way this can be done without having to use project settings, make files etc?
Added:
Why do I want to do this.
Well this gets into another subject which is probably my next question - working with the IDE.
I have to provide many examples in one project. They are simple single files that demonstrate different ways of doing things and each one should really be a different executable EXample1.exe, Example2.exe.
I only want to paste the source code or hand someone a SINGLE file with everything needed to make the example executable (on a web forum for example. I do not want to attach a 3.6MB project folder just to get a different executable name!
Compiling transcends source code. Source code only exists, and something has to take it and make something of it. Anything you do in source code is really just going to be a directive to the compiler. You might as well use project settings. This stuff isn't standard, because the standard only covers behavior and definitions of source code, not compilers.
g++ takes the output file as a parameter: g++ -o myexe.exe main.cpp. What should it do if it comes across a "output should be this!" directive in the source code?
Same with cl (Visual Studio), it passes the output setting into the command line.
Not to say it's impossible, but I doubt it's worth it to try and come up with a way to do it, let alone make it standard.
To use the linker pragma comment, the output file must NOT be specified in the linker section of the project properties:
project -> properties -> Linker -> General -> Output File
Delete the entry: $(OutDir)\$(ProjectName).exe
then the prama statement will work:
pragma comment(linker, "/out:mycool.exe")
Thanks to JC for the walkthrough
Specifying a complete path is not possible
http://social.msdn.microsoft.com/forums/en-US/vcgeneral/thread/11a06ecd-dcca-4d81-8626-ba0c5a1835c1/
but the work around is:
What I do is have a header file loacated somewhere near the library file, this header will include the pragma line.
pragma comment(lib, FILE "../libs/mylibary.lib")
if FILE is "C:\Project\SharedLibs\Xvid\latest.h"
then the pragma will include
"C:\Project\SharedLibs\Xvid\libs/mylibary.lib" once it has normalized the uri to remove the ..'s
this will always cause the pragma to include the library with an absolute path created from the path of the accompanying header.
I use this system to include a single header in a project and regardless of the relative paths between the lib and project the lib will always be included cleanly.
Added:
The full path can be specified as long as it is 8.3 format. This can present problems for a path like:
C:\Program Files\Abyss Web Server\htdocs\
Program files is commonly Progra~1
but a folder name with a space is more tricky. In this case it becomes AbyssW~1
The \ must be escaped resulting in \ producing a working pragma of:
#pragma comment(linker, "/out:C:\\Progra~1\\AbyssW~1\\htdocs\\MyApp.exe")
as kibibu showed:
#pragma comment(linker, "/out:\"C:\\Program Files\\Abyss Web Server\\htdocs\\MyApp.exe\"")
also works
If you don't want to stray too far from a stock Visual C++ installation, you should consider using NMake. It can integrate with the IDE using project files, but it can also simply be run from the command-line very easily. It's also far more lightweight than project files for generating an arbitrary number of simple and similar executables.