LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2011, 07:39 PM   #1
OnlySolitaire
LQ Newbie
 
Registered: Feb 2011
Posts: 6

Rep: Reputation: 0
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.
 
Old 03-02-2011, 12:56 AM   #2
Awatto
Member
 
Registered: Aug 2003
Location: Halifax, NS
Distribution: Debian, Gentoo, Ubuntu, Fedora
Posts: 128

Rep: Reputation: 30
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.
 
Old 03-02-2011, 01:33 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.
 
  


Reply

Tags
c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multifile Rule definition in Makefile sahil.jammu Linux - Newbie 1 08-29-2010 08:45 AM
compile multiple modules in Makefile sharathkv25 Programming 1 04-11-2007 04:28 PM
configure Makefile to compile for multiple platforms StargateSteve Programming 0 03-26-2007 12:54 PM
duplicated definition for a target in Makefile George2 Programming 2 05-07-2006 05:01 AM
multiple definition ? lackluster Linux - Software 1 11-27-2003 06:29 PM


All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration