How to compile .h and .hpp in Visual Studio 2013 - c++

first post and I'm pretty new to programming compared to most of you. I'm at a University and we have virtual linux machines that we usually code on and compile to. We've always had a command to instantly compile all programs in a file. Now I'm trying to be a big boy and use Visual Studio 2013 on my home computer. I'm having problem compiling. When I compile, it will only read through the main.cpp, even if I #include "blah.h" at the top. How do I set the compiler to check everything in my project.
I also don't get the point of the "build". I've never run across this before on our linux machines. We just write what we want, #include everything we use, and the compiler just reads it all and does it's job.
I'm a noob so don't judge. I am pretty good with all my knowledge, classes, pointers, data structures, I'm just a complete noob when it comes to compilers and IDEs. What is the difference in g++ compiler? My professor never talked much about IDEs and compilers aside from easy to use linux machine we have on campus.
TL;DR: How do I compile .hpp and .h in VS2013, always used easy peasy linux machine on campus.

You have to set up a project: File -> New Project, Under Visual C++ pick General and then create an Empty Project. Assume it is called Test1. Then, head over to the Solution Explorer, right click on Test1 and under Add, there should be Add Existing Items. Add your files and you are good to go!

You might want to read this question:Do I need to compile the header files in a C program?
In short, there is no need to compile the .h files since these .h files shall be included in the .cpp files.
In order to check everything in your project, the .h file shall be included in you main.cpp. In this case, the visual studio compiler shall precompile the cpp file which means the #include "test.h" will be replaced by the content of test.h .
What is the difference in g++ compiler?
"GCC has special treatment for files with .h extension when they are supplied to the compiler as command-line arguments."

Related

Why Visual C++ compiler compiles unused classes into executable?

I probably misunderstand how the compilers work,but I have the following situation.I have a VC++ 2012 project.The project contains an API with hundreds of classes.Now, I create 'main' entry point file where I just use some std stuff.The main doesn't include even a single header from the API.Yet when I compile I see that pretty everything from the API is getting compiled into the executable?Why does it happen?Is it something I should configure in the project properties?
Source files that are part of the project are compiled also if "unused" through headers and references from your main routine.
If you don't want those compiled simply remove them from the project. The compiler might be able to optimize everything away but that doesn't mean they're not compiled if part of the project first.

How to write C++ code that works between IDE?

I am new to C++ from a C#.NET and Visual Studio background (and some Objective-C / XCode).
I want to write cross-platform (Windows, OS X, Linux) application with C++. I started with TextMate on my Mac and Notepad on my Windows, and compile the code using g++ on command line/terminal to build the executables on its platform.
I got confused when I started using IDE (namely VC++, XCode, and Eclipse). I tried all three and created projects before pasting my code in it. If I start editing in one IDE, I do not know how to get the eventual piece of code to compile on different platform.
For example, if I have a class file - foo.cpp. In Eclipse, it would generate foo.cpp (within it a foo.h, foo::foo()) and a separate foo.h header file (within it a FOO_H_ file etc). In Xcode, it generates a folder Foo constains main.cpp and foo.l) - that's it. Why the difference?
I though C++ code is just C++ code which produce platform specific executables if compiled on that platform. (e.g. compile on Mac, it becomes a Mac executables; if compiled on Windows, it becomes a Windows executables).
It seems, once I started using IDE, my C++ code automatically become unportable, due to all these auto generated files that I have no understanding about. I try reading documentation on XCode, Eclipse, and Visual Studio at the same time, and it confuses me even further. I just want to write C++...
I am also following Stroustrup's classic text to pick up C++ and in the book, nothing like header or source of .h or _H_ files were mentioned, as of why all these files were generated.
Besides using Notepad/Textmate + G++ compiler, how can I write pure, portable C++ program in an IDE that can be cross-platform, or is it even possible?
The default (=most commonly used) file structure for a c++ class called Foo consists of a header file called foo.h which will contain the class definition of Foo, and a foo.cpp which contains the definition of the methods of Foo.
Often all the header files are put into a seperate folder called include.
So the general approach to have the same file/folder structure, which can be used for more than one IDE is:
create the folder structure manually (e.g. include, src and make folders inside your project folder MyProject, the project files for IDEs would go into make, .h files into include, and .cpp files into src)
create the .h and .cpp files for your classes manually using the explorer/finder/...
use "add existing file" or something equivalent to add those files to the project file of your IDE.
as Rup pointed out, for Visual Studio, one should use the "empty project" preset, or it will add some header files you dont want
Why not use 1 IDE for all? Eclipse is cross platform IDE
Use CMake or other meta-buildsystem
Use Boost and Qt to abstract away as much of the platform as possble.
CMake lets you generate project files for most IDE:s that mean when switching between platforms and IDE:s you just tell CMake to generate new project-files for that IDE you use on that platform.
Easiest to get platform-idependent code on windows, linux and mac is to use QtCreator as IDE, it also has a lot of good tutorials. http://qt.nokia.com/downloads

Problems creating a static C++ library

Greetings fellow programmers! I am trying to create a C++ library to use in other projects. This library is to be a static library that will be linked to other projects.
Problem: The compiler doesn't seem to catch errors that it otherwise would if it were configured as an executable program under Project->Properties>General. In fact I purposely made a syntax error and the compiler
gave me a success regardless.
Is there a way to force the compiler to check everything while building a static lib?
Build Environment: Visual Studio 2010
EDIT: As it turns out, it was a rather simple, yet incredibly subtle mistake. It turns out that Visual studio was treating some of my .cpp files as header files. Because of this, the compiler was not running through the code at all (no one includes .cpp files). When adding files to the library, I must have accidentally selected header instead of C++ file. I thought Visual Studio would only rename the header file. I guess I was wrong!
This is nonsensical. It cannot happen. What is probably happening is that you think that the compiler is compiling your source file, (the one with the syntax error,) but it does not. Perhaps you have not added your source file to your project.
(Then again, C++ has a pretty quirky syntax; I hope you are sure that what you made was in fact a syntax error, and not a syntactically valid language construct.)
If you have more than one project in the workspace (solution) like exe and lib, make sure you click on the exact one to build or set it as the startup project (showing up in bold letters) if you hit the F7 button.

Visual Studio 2010, Intellisense and PCH: what are the alternatives to ugly stdafx.h?

I recently switched to Visual Studio 2010 and for Intellisense not to take half a minute to show up when using boost libraries, Microsoft's suggestion seems to use precompiled headers.
Except that I never used them before (except when forced to by Ugly ATL Wizards (TM)), so I searched around to figure out how they work.
Basically, the Big Centralized stdafx.h approach seems plain wrong. I never want to include (even cheaply) a whole bunch of header files in all my sources. Since I don't use windows libraries (I make C++/CLI higher level wrappers, then use .NET for talking to the outside world), I don't have "a whole truckload of non-changing enormous headers". Just boost and standard library headers scattered around.
There is an interesting approach to this problem, but I can't quite figure out how to make this work. It seems that each source file must be compiled twice (please correct me if I'm wrong): once with /Yc and once with /Yu. This adds burden on the developper which must manually tweak the build system.
I was hoping to find some "automatically generate one precompiled header for each source file" trick, or at least some "best practices", but most people seem happy with including the world into stdafx.h.
What are the options available to me to use precompiled headers on a per source file basis ? I don't really care about build times (as long as they don't skyrocket), I just want intellisense to work fast.
For starters, you are reading the article wrong. Every file is NOT compiled twice. The file stdafx.cpp gets compiled once with /Yc (c, for create) before anything else and then every other file in your project gets compiled once with /Yu (u, for use) and imports the result of the previously created saved state from stdafx.cpp.
Secondly, the article is 7 years old and is talking about VC++ 6, so you should start off distrusting it. But even assuming the information in it still applies to VC++ 2008 or 2010, it seems like bad advice. The approach it recommends using /pragma hdrstop is solution looking for a problem. If you have headers that contain things you don't want in every file, then they simply shouldn't go in your pre-compiled header.
Your problem basically seems to be that Intellisense is slow for Boost in VS2010? I don't have a direct solution for this problem, but could Visual Assist X be an option for you? I have used it in various versions of Visual Studio now and with great pleasure. Not a direct solution, but it might work for you.
Precompiled headers aren't too bad if you use them properly.
Don't use them as a replacement for proper and precise #includes, but as a way to speed things up. Achieve this by making the precompiled header do nothing in release builds, only speeding stuff up in debug.
You are wrong, each file is only compiled once. You have one .cpp file that is compiled with /Yc and the rest are compiled with /Yu. The file with /Yc, which is stdafx.cpp by default, contains one line, #include "myMainHeader.h" (changed the name from the default) All other .cpp files must start with #include "myMainHeader.h" When your /Yc file is compiled, the entire internal state of the compiler is saved. That file is loaded when each of your other files is compiled. That is why you must start with including the PCH, so that the /Yu option doesn't change the result of compilation, only the time. Xcode does not make this requirement and will use a PCH regardless of if your .cpp file starts with the right include directive. I have used libraries that relied on this and could not be built without PCH.

My Visual C++ compiler compiles out of date source

I'm a beginner starting to use Microsoft Visual C++ Express 2010 for Windows Programming. I've created a new C++ application using native code, not managed or MFC. I had Visual Studio create for me the basic windows code to create one window with simple menus (chat.cpp). I modified this file and I was able to compile correctly and see my changes take effect.
Now I've added one more source file to the solution (intro.cpp) which I include in my header and call from within chat.cpp. This seems to work just fine, but with one problem. The compiler doesn't seem to be applying my code changes occasionally: I make various edits, recompile, and the "old" code seems to be running. Then, after a while, after I make only a tiny modification, the compiler seems to "catch up" and it runs the new code including all the previous changes I had made.
Is there some kind of cache? Do I need to tell the compiler which files to compile and which ones to just link from object files? Did I make a mistake when I added the file to the solution?
Does intro.cpp have a header file? is that in your solution too?
It's hard for me to imagine that this area of such a mature IDE has a bug here, so I would examine your file list first. Make sure that the Solution Explorer shows all the files you have added and are editing. This is the list that VS uses to determine rebuild is needed.
EDIT: I admit it's not clear to me from your description why it would fail right now. However, typically header files do not include code, it works the other way around (except for class template header files, such as the STL headers). There is some discussion about pros and cons here. The most compelling argument to me in favour of code including headers rather than vice versa is that the header file contains the interface, while the code file contains the implementation.
I would try restructuring your code to a more traditional structure where intro.cpp includes intro.h and any others it needs, and the same with chat.cpp. Then your compilation units are intro.cpp and chat.cpp, and they depend on the associated header files, so provided they are properly listed in the SOlution Explorer, all should work. You can clean up the build dependencies to avoid dups and reduce build time once you have it working as you wish.