LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using makefile - 'Multiple Definition' compile errors (https://www.linuxquestions.org/questions/linux-newbie-8/using-makefile-multiple-definition-compile-errors-865820/)

OnlySolitaire 03-01-2011 06:39 PM

Using makefile - 'Multiple Definition' compile errors
 
As part of an assignment, our class was given a small set of c++ classes which are compiled using a makefile.

As part of the assignment, I have created a namespace and saved in it a .cpp file. The functions, constants and typedefs from the namespace are now used in one of the original files, and so I added an <#include MyFile.cpp> to the file I modified, in addition to a using MyNamespace;


When I try to use the original makefile, the compiler gives a lot of errors about multiple definitions of every function in the namespace.

What do I need to do to resolve this? Do I need to change the makefile or one of the files themselves? I am new to Linux programming and makesfiles.


I can post the actual files if needed.

Awatto 03-01-2011 11:56 PM

Hi OnlySolitaire,

The Programming forum would have likely been a better choice. (Many Linux users know very little or even no programming, and this isn't really a Linux-newbie question)

In any case, it is hard to tell exactly what is going on without seeing the source for your makefile, cpp files, hpp files, and the errors. Perhaps use pastebin or something similar for that.

Dark_Helmet 03-02-2011 12:33 AM

Quote:

Originally Posted by OnlySolitaire
and so I added an <#include MyFile.cpp> to the file I modified

Maybe you were using a shorthand, but just in case...

If you created a header file to be included by other cpp files, then 99% of the time, it should be included like this:
Code:

#include "MyFile.hpp"
So-called system header files use something like "#include <vector>" -- these header files are stored in pre-determined directories on the computer you're working with. Your header file is extremely unlikely to be in one of those directories.

Also, by convention, headers have a "hpp" extension and contain only #defines, class definitions, and other data type definitions--no executable code (i.e. functions) at all.

Again, by convention, a header file should have something like this:
Code:

#ifndef MYFILE_HPP
#define MYFILE_HPP

/* Place the content of your header file here */

#endif

This prevents re-definition errors when you have multiple cpp files #include'ing the same hpp file.


All times are GMT -5. The time now is 05:12 PM.