Good day to you all people.

I personally like to use Visual Studio and Dev C++ to develop my c++ apps. Recently, I was introduced to a new environment, Textpad as the editor and Cygwin as the compiler.

I found interesting that in Visul Studio and Dev C++ you are not allowed to link or include .cpp files, and in Cygwin you can actually do that.

For example: if you have a header file named shape.h and the implementation/definition of the members of the header file are in a different file named shape.cpp; in Visual Studion and Dev C++, to use the members of shape in the main file(main.cpp, the file that has the main function) you have to include the shape.h file, otherwise it will result in errors. While in Cygwin it is the opposite, you have to actually include the shape.cpp file.

Any comments about how this 3 environments/compilers(Visual Studio, Dev C++ and Textpad/Cygwin) handle file linkage??? Is there any way to include '.h' files in Cygwin and '.cpp' files in Visual Studio/Dev C++??

Regards.....

Normally, your IDE handles the Makefile for you. Since you said cygwin, i assume you know about Automake, Autoconf & Libtool. These tools will also configure your Makefile and do the necessary file linkages. However, there are licensing issues to consider when using the above tools...

You should never include a .cpp file in another file. .cpp files should include .h/.hpp files and then all of those .h/.hpp and the .cpp file get compiled together to make an object file. This should happen for each .cpp file you have in your project. Once that happens then all of those objects should be linked together by the linker to make the final executable.

Have read more than 3 books and 5 tutorials and all say that only include .h files should be included, but if you use cygwin it won't compile.

Since I work most with Visual Studio and Dev C++ I have to include .h files. But why doesn't Cygwin allow we to have the declaration in a .h, the implementation in a .cpp and then link the .h to the main file!!!..

There must be some reason for that.....!!

How does the Cygwin compiler differs from the others in the way it links files!!!

I think you have many misconceptions about the compilation and linking process of C/C++ sources. I suggest you read my comprehensive tutorial on the subject.

Like NathanOliver said, there is (almost) no reason to ever #include cpp files. Each cpp file should be compiled separately (they each should have the #include headers necessary to compile correctly), each forming an "object file". All object files are then linked together to form the final executable(s) for your project.

For example, when you are using an IDE like Visual Studio or Dev-C++, you would typically add all the cpp files to your project and the IDE takes care of compiling them all and linking them all together. When you use a more bare-bone environment, like Cygwin, then you have to do that up manually (when invoking the compiler / linker) or you have to set it up in a build-system tool (like Makefiles, Autoconf, or CMake). CMake is by far the best tool to use. See this tutorial on how to use cmake. CMake is the most widely used primary build-system because it works well in the command-line environments (on all OS types), it is supported by most IDEs, and it can generate "projects" or "solutions" to be loaded into the IDEs that don't support CMake natively (such as Visual Studio), making it the ideal cross-platform universal build-system (set up one project configuration that will work everywhere, all OS types, all IDEs, all compilers).

Okey, so if you have Cygwin installed please try to make this program run, if successful please share how you managed to do so, "using Cygwin".....

............Main.cpp............................................
#include"shape.h"
//
int main()
{
    shape shape1;
    shape1.printVolume();
    return 0;    
}
//
...........shape.h................................................
//
#ifndef Shape_h
#define Shape_h

    class shape
    {
        private:
            double volume;
        public:
            void printVolume();//function to print the volume
            shape();//default constructor
    };
#endif
//
...........shape.cpp...................................................
#include <iostream>
using namespace std;
#include "shape.h"
//
shape::shape()
{
    volume = 3.3;
}
void shape::printVolume()
{
    cout << volume << endl << endl;
}

You can get this to compile by using this command:

$ g++ -Wall Main.cpp shape.cpp -o ShapeTest.exe

As far as I know, this should work in cygwin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.