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

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

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.

Cross platform compilation of C++/Qt project

I am a C++ novice. I have found an open source project written in C++/Qt. I want to make slight modifications and compile for both Windows and Mac. Is it typically that I will need to change my code to support either platform, or can it usually compile for both platforms out of the box?
If the source code uses clean C++ and Qt then it should compile (given that you have a Qt project file or Cmake file). If the code uses any platform specific APIs, you will have to update those first (either with multiple pieces of code, or with other cross-platform APIs)
In most cases, you don't have to change your code to support both OS s excepting cases described above. You simply have to open folder with ur qt project and open .pro file. The you should compile it and receive ur OS build.

How to compile .h and .hpp in Visual Studio 2013

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."

Redeveloping MFC application

I've got a program written in c++ and have to make some modifications to it. Howerver, I haven't got any solution file or a like, just a bunch of .h, .cpp, .obj, .dpb, .dll, .mdi, .dfm, .res, .tds, .bpl files and an .exe file.
So I can start an application, see what it's doing and what the controls are, but when it comes to adding changes trying do to map behaviour of UI to source code files seems just to tedious.
Is it possible to recreate some kind of solution file or just to simply the process? (I am using VS 2012 and an app was written in 2002).
These file extensions indicate the source is from a Borland IDE. That .mdi file I think signifies Borland's multiple-document-interface... I'll venture to guess this project would be quite difficult to "recompile" using Visual Studio. I don't even see a .rc (resource file) in your list. Rewriting may be the easiest path, depending on the complexity of the application...

Does Visual Studio 2008 use make utility?

I have checked in buid directory and have not found makefile there. How does Visual Studio 2008 buid the project? Does it use makefile?
The NMAKE utility has been distributed with Visual C++ since back when it was called Microsoft C/C++ Optimizing Compiler, and is very similar to Unix make. Previous versions of the IDE actually used NMAKE makefiles, but this isn't true anymore. You can write NMAKE makefiles yourself if you want, but it sounds like you want to know what the IDE does.
Starting with VS2010, the build system changes to MSBUILD, which bertelmonster mentioned. But not in VS2008.
In VC++ 6.0, C++ projects have their own build engine integrated into msdev.exe.
In VS2002 - VS2008, it's a separate tool, VCBUILD. But you can still invoke it via the main IDE, devenv.exe, see the /BUILD option, and devenv is the best way if you have inter-project dependencies in your solution.
VS9 doesn't use makefiles by default the way Linux-flavored IDEs might. Instead, VisualStudio uses 'solution' and 'project' files. A Project file (*.vcproj for C/C++ projects) is basically a replacement for makefiles, and contains instructions, compiler directives, flags and everything else needed to compile a single 'project'. In this parlance, a project is a single output file, such as an EXE or DLL. But this same mechanism can be used to produce any kind of output, including TLBs, text files, widgets and ice cream cones (if your machine has the capable hardware :) )
A 'solution' is a collection of projects, and the solution file (*.sln) contains lists of projects needed to build an entire application suite, typically. It also contains dependancy information, so that the projects are built in the correct orders.
Solution and project files are human-readable text, but in the VS world you would virtually never want to edit these files yourself the way you would tweak a makefile by hand. Instead, you would use the IDE to change compiler flags, preprocessor directives, output directories, and all the rest.
That is how VS works by default, but VS is also capable of using makefiles in much the same way as Linux-flavored IDEs. It still uses solution files in this case, so you can mix projects that use makefiles with projects that use project files within the same solution. The VS IDE is actually quite powerful in this regard, and gives you the ability to do pretty much whatever you want. This power however comes with a price -- with so many features and capabilities available in the IDE, it can be rather complex and takes what you might think is an unwarranted ammount of user brainpower to fully understand.
If you want to create a makefile project, you can do so by doing File>New>Project... and then selecting Makefile Project from the main Visual C++ list of project templates.