LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2009, 04:39 PM   #1
icarus127
LQ Newbie
 
Registered: Aug 2007
Distribution: Slackware 12.2
Posts: 22

Rep: Reputation: 15
Help with a makefile


I've been working at learning how to write makefiles and I'm a bit stuck. I've got a fairly simple project that will eventually be a framework for TCP/IP Communications in evolutionary algorithms that I've been using Eclipse to develop. For personal development I want to learn how to write makefiles by hand but have gotten kind of stuck Here is the makefile as it stands now:

Code:
CC := g++

LDFLAGS := -Iconnection/ -Iconcurrency_control/ -Imailbox_files/ -Iport/ -Ithread_files/ 

CXXFLAGS := -c

VPATH := connection:concurrency_control:mailbox_files:port:thread_files

objects := main.o connection.o mutex.o semaphore.o connection.o  \
             tcpIpSocket.o  thread_asm.o

connectionTestPlatform : $(objects)
	$(CC) -o connectionTestPlatform $(objects)
  
main.o : ConnectionTestPlatform.cpp   Connection.h ConnectionObserver.h TestPort.h TcpIPSocket.h
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
connection.o : Connection.cxx Port.h Connection.h  ConnectionObserver.h Thread_ASM.h 
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
mutex.o : Mutex_ASM.cpp Mutex_ASM.h
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
semaphore.o : Semaphore_ASM.cpp Semaphore_ASM.h
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
#connectionObserver.o : ConnectionObserver.h
#	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
#mailbox.o : Mailbox_ASM.h
#	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
tcpIpSocket.o : TcpIPSocket.cpp  Port.h TcpIPSocket.h
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
testport.o : TestPort.h Port.h
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@
  
thread_asm.o : Thread_ASM.cpp  Thread_ASM.h Mutex_ASM.h
	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@

#port.o : Port.h
#	$(CC) $(CXXFLAGS) $(LDFLAGS) $< -o $@

.PHONY : clean
clean :
	rm -f $(objects) connectionTestPlatform
Port and Mailbox are a virtual class and a template class respectively. I get these errors when I run make:

Code:
connection.o: In function `Connection::ThreadStart(void*)':                                            
Connection.cxx:(.text+0x0): multiple definition of `Connection::ThreadStart(void*)'                    
connection.o:Connection.cxx:(.text+0x0): first defined here                                            
connection.o: In function `Connection::TransmitData(char const*, int)':                                
Connection.cxx:(.text+0x20): multiple definition of `Connection::TransmitData(char const*, int)'       
connection.o:Connection.cxx:(.text+0x20): first defined here                                           
connection.o: In function `Connection::IsActive()':                                                    
Connection.cxx:(.text+0x4e): multiple definition of `Connection::IsActive()'                           
connection.o:Connection.cxx:(.text+0x4e): first defined here                                           
connection.o: In function `Connection::GetConnectedHostIdentifier()':                                  
Connection.cxx:(.text+0x5e): multiple definition of `Connection::GetConnectedHostIdentifier()'         
connection.o:Connection.cxx:(.text+0x5e): first defined here                                           
connection.o: In function `Connection::NotifyAllObservers(char*, int)':                                
Connection.cxx:(.text+0x202): multiple definition of `Connection::NotifyAllObservers(char*, int)'      
connection.o:Connection.cxx:(.text+0x202): first defined here                                          
connection.o: In function `Connection::AttachObserver(ConnectionObserver*)':                           
Connection.cxx:(.text+0x322): multiple definition of `Connection::AttachObserver(ConnectionObserver*)' 
connection.o:Connection.cxx:(.text+0x322): first defined here                                          
connection.o: In function `Connection::~Connection()':                                                 
Connection.cxx:(.text+0x37c): multiple definition of `Connection::~Connection()'                       
connection.o:Connection.cxx:(.text+0x37c): first defined here                                          
connection.o: In function `Connection::~Connection()':
Connection.cxx:(.text+0x490): multiple definition of `Connection::~Connection()'
connection.o:Connection.cxx:(.text+0x490): first defined here
connection.o: In function `Connection::~Connection()':
Connection.cxx:(.text+0x5a4): multiple definition of `Connection::~Connection()'
connection.o:Connection.cxx:(.text+0x5a4): first defined here
connection.o: In function `Connection::Connection(Port*, int)':
Connection.cxx:(.text+0x6b8): multiple definition of `Connection::Connection(Port*, int)'
connection.o:Connection.cxx:(.text+0x6b8): first defined here
connection.o: In function `Connection::Connection(Port*, int)':
Connection.cxx:(.text+0x7ee): multiple definition of `Connection::Connection(Port*, int)'
connection.o:Connection.cxx:(.text+0x7ee): first defined here
connection.o: In function `Connection::ReceiveThreadBody()':
Connection.cxx:(.text+0x924): multiple definition of `Connection::ReceiveThreadBody()'
connection.o:Connection.cxx:(.text+0x924): first defined here
semaphore.o: In function `Semaphore_ASM::post()':
Semaphore_ASM.cpp:(.text+0x11): undefined reference to `sem_post'
semaphore.o: In function `Semaphore_ASM::pend()':
Semaphore_ASM.cpp:(.text+0x47): undefined reference to `sem_wait'
semaphore.o: In function `Semaphore_ASM::~Semaphore_ASM()':
Semaphore_ASM.cpp:(.text+0x87): undefined reference to `sem_destroy'
semaphore.o: In function `Semaphore_ASM::~Semaphore_ASM()':
Semaphore_ASM.cpp:(.text+0xc3): undefined reference to `sem_destroy'
semaphore.o: In function `Semaphore_ASM::~Semaphore_ASM()':
Semaphore_ASM.cpp:(.text+0xff): undefined reference to `sem_destroy'
semaphore.o: In function `Semaphore_ASM::Semaphore_ASM(int)':
Semaphore_ASM.cpp:(.text+0x141): undefined reference to `sem_init'
semaphore.o: In function `Semaphore_ASM::Semaphore_ASM(int)':
Semaphore_ASM.cpp:(.text+0x16f): undefined reference to `sem_init'
thread_asm.o: In function `Thread_ASM::~Thread_ASM()':
Thread_ASM.cpp:(.text+0x12e): undefined reference to `pthread_kill'
Thread_ASM.cpp:(.text+0x142): undefined reference to `pthread_join'
thread_asm.o: In function `Thread_ASM::~Thread_ASM()':
Thread_ASM.cpp:(.text+0x1e4): undefined reference to `pthread_kill'
Thread_ASM.cpp:(.text+0x1f8): undefined reference to `pthread_join'
thread_asm.o: In function `Thread_ASM::~Thread_ASM()':
Thread_ASM.cpp:(.text+0x29a): undefined reference to `pthread_kill'
Thread_ASM.cpp:(.text+0x2ae): undefined reference to `pthread_join'
thread_asm.o: In function `Thread_ASM::Join()':
Thread_ASM.cpp:(.text+0x385): undefined reference to `pthread_join'
thread_asm.o: In function `Thread_ASM::Start(bool)':
Thread_ASM.cpp:(.text+0x3f9): undefined reference to `pthread_create'
thread_asm.o: In function `Thread_ASM::Thread_ASM(void* (*)(void*), void*, unsigned int, int, int, int, int)':
Thread_ASM.cpp:(.text+0x4bf): undefined reference to `pthread_attr_setstacksize'
thread_asm.o: In function `Thread_ASM::Thread_ASM(void* (*)(void*), void*, unsigned int, int, int, int, int)':
Thread_ASM.cpp:(.text+0x5f7): undefined reference to `pthread_attr_setstacksize'
So for some reason every single function in the Connection class has been defined multiple times. I assume because it's included as a dependency for multiple targets, but I've seen this in numerous examples that apparently work.
Everything in libpthread is undefined because I'm not linking in the library. I've tried adding the library options to the LDFLAGS variable but that doesn't seem to work either.
Can anyone give me pointers on where to go from here? Thanks much
 
Old 03-31-2009, 07:03 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by icarus127 View Post
I assume because it's included as a dependency for multiple targets, but I've seen this in numerous examples that apparently work.
This looks more like a broken header file. Do you have something like this at the top of "Connection.h":
Code:
#ifndef __CONNECTION_H
#define __CONNECTION_H
and a line like this at the bottom
Code:
#endif // __CONNECTION_H
That makes sure that the file is included once, and only once, no matter how many times the compiler sees
Code:
#include "Connection.h"
Also, you need to add "-lpthreads" to your LDFLAGS.
 
Old 03-31-2009, 07:31 PM   #3
icarus127
LQ Newbie
 
Registered: Aug 2007
Distribution: Slackware 12.2
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David1357 View Post
This looks more like a broken header file. Do you have something like this at the top of "Connection.h":
Code:
#ifndef __CONNECTION_H
#define __CONNECTION_H
and a line like this at the bottom
Code:
#endif // __CONNECTION_H
That makes sure that the file is included once, and only once, no matter how many times the compiler sees
Code:
#include "Connection.h"
Also, you need to add "-lpthreads" to your LDFLAGS.
I do have the #ifndef/#define block in all my headers files, and I've tried the -lpthreads in LDFLAGS to no avail :\ I've also tried -lrt which is what I've used in other projects that use posix semaphores and mutexes but this has also not worked. I get this message:

Code:
g++: -lpthreads: linker input file unused because linking not done                                     
g++: -lrt: linker input file unused because linking not done

Last edited by icarus127; 03-31-2009 at 07:33 PM.
 
Old 04-01-2009, 01:59 PM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by icarus127 View Post
I do have the #ifndef/#define block in all my headers files
I do not think I can help you diagnose the problem further without seeing the contents of Connection.cxx and Connection.h.

Quote:
Originally Posted by icarus127 View Post
Code:
g++: -lpthreads: linker input file unused because linking not done                                     
g++: -lrt: linker input file unused because linking not done
Yes. I forgot you were using g++ as your linker. You will have to pass any linker flags using "-Wl,<options>". For example
Code:
LDFLAGS += -Wl,"-lpthreads -lrt"
should work. If that does not work
Code:
LDFLAGS += -Wl,-lpthreads -Wl,-lrt
should.
 
  


Reply



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
Is it mandatory to have the name of the makefile as 'Makefile' for kernal module comp narender.d Linux - Kernel 3 05-29-2009 06:26 AM
automake, makefile, makefile.in and makefile.am Fond_of_Opensource Linux - Newbie 1 09-12-2006 08:35 PM
what is the meaning of Makefile,Makefile.am,Makefile.in cynthia_thomas Linux - General 3 12-08-2005 05:00 AM
how to get (makefile -f makefile )output into the textview widget in Pygtk sailu_mvn Programming 3 02-28-2005 03:57 AM
generate Makefile from Makefile.in without calling ./configure ? chris78 Programming 2 05-02-2004 12:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 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
Open Source Consulting | Domain Registration