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 10-13-2015, 02:30 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Compiling C code using makefile


Hi
I written four C programs each does different function when received message from client
Common thing for all C programs are socket which act as server and execute function in thread.

Now what I feel there are repitition of code.I want to avoid this.
For example Include directory will be there were wrapper is written for socket etc.
Also I want to use makefile

Please show me some example code so that I can use as reference.
 
Old 10-13-2015, 02:44 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'm not sure what the question is, but here's the manual for GNU make:
http://www.gnu.org/software/make/manual/make.html
 
1 members found this post helpful.
Old 10-13-2015, 08:05 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Firstly it sort of matters if these four files link to create one image or if you intend to create multiple targets. As HMW notes, read up on Makefiles and begin with rudimentary ones. There are plenty of examples for how to make the most simple of Makefiles.

And a VERY IMPORTANT make file thing you need to know is PUNCTUATION. Note that when you indent you NEED TO USE TABS AND NOT WHITESPACES. I recommend you use a Linux or Unix editor to create your Makefiles and also ensure that the editor is not configured to replace TAB with spaces.

I have a simple legacy Makefile which I refer to all the time for the basics as well as my list of GCC compiler flags that I like to retain and use. They improve my code because they check for lots of warning conditions and thus help me to not create suspect code. I'll be happy to post that example, but I feel you first ought to make a reasonable attempt to show what you've tried.
 
Old 10-13-2015, 09:44 PM   #4
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
The best way to create a makefile is to look at some makefiles. Maybe start with the makefiles used to build make. Hopefully they are up to date and correct. Put all your code in one file and remove duplicate code?
 
Old 10-14-2015, 12:50 AM   #5
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
Thanks all

@rtmistler I have one makefile
I made directories as Source, Include, Lib
Source :-cpp files where functions are written
Include:-header files for source cpp
Lib:- After compilation .o files should go in this directory

Source,Include,Lib will be common for all 4 programs I mentioned in first post

Apart from that my first main application appdownload.cpp file will be in project directory along with its include.h file.
Code:
CC		= g++
CFLAGS		= -g -c -Wall 
LDFLAGS		= -lpthread
#SOURCES		= server.cpp tcpstream.cpp tcpacceptor.cpp
SOURCES		= appdownload.cpp
INCLUDES	= -I.
OBJECTS		= $(SOURCES:.cpp=.o)
TARGET		= AppDownload

all: $(SOURCES) $(TARGET)

$(TARGET): $(OBJECTS) 
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
	$(CC) $(CFLAGS) $(INCLUDES) $< -o $@

clean:
	rm -rf $(OBJECTS) $(TARGET)
Please guide me from here

Last edited by srinietrx; 10-14-2015 at 01:14 AM.
 
Old 10-14-2015, 01:52 AM   #6
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
I managed to write working makefile

Code:
CC		= g++
CFLAGS		= -g -c -Wall 
LDFLAGS		= -L/usr/local/lib
LDLIBS      = -lcurl -lpthread
#SOURCES		= server.cpp tcpstream.cpp tcpacceptor.cpp
SOURCES		= appdownload.cpp Source/tcpstream.cpp Source/tcpacceptor.cpp Source/tcpconnector.cpp
INCLUDES	= -I. -I Include/
OBJECTS		= $(SOURCES:.cpp=.o)
TARGET		= appdownload

all: $(SOURCES) $(TARGET)

$(TARGET): $(OBJECTS) 
	$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@

.cpp.o:
	$(CC) $(CFLAGS) $(INCLUDES) $< -o $@

clean:
	rm -rf $(OBJECTS) $(TARGET)
 
Old 10-14-2015, 08:10 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I think it's an excellent effort and in fact there's little you need assistance with.

Here are some added suggestions:
  1. Start the file with #!/bin/sh, it will help you to be able to use shell variables and commands, for instance I declare START to be `pwd` in case I need to move around directories as part of my make flow. Otherwise I think it will assume a shell no matter what anyways, but I prefer to make that a specific declaration.
  2. My CFLAGS add:
    Code:
    -Wformat=2 -Werror -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn
  3. Rather than declare my sources I declare the objects and a dependency for their compilation, mine is for C versus CPP:
    Code:
    OBJS= something.o something-else.o
    
    .o:    %.c
            $(GCC) $(CFLAGS) $(INCLUDES) $(DFLAGS) -c &<
    
    target: $(OBJS)
            $(GCC) $(OBJS) (LFLAGS) -o $@
  4. The only other minor suggestion is that I use only "rm -f" not "rm -rf" for my clean. I'm doubtful -rf is needed and I'd stay away from recursively removing directories just in case something weird happens and suddenly "Source" gets removed, along with all the underlying code source files.
I think at this point a lot of this are style differences between one programmer versus another.

What I do is add incrementally new ideas as I see them in other examples or develop them when I need too.

Last edited by rtmistler; 10-14-2015 at 08:24 AM.
 
Old 10-14-2015, 08:24 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Also do not overlook handy tools like Automake.

If you use these tools to discover dependencies and to construct your makefiles, it is much easier to be certain that your makefiles remain accurate as the program evolves.

Last edited by sundialsvcs; 10-14-2015 at 08:26 AM.
 
Old 10-14-2015, 09:50 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by rtmistler View Post
Start the file with #!/bin/sh,
I don't think adding a #! to a makefile has any effect. You can set the SHELL make variable to explicitly choose the shell used to execute recipes.

5.3.2 Choosing the Shell
 
  


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
Compiling a gd-lib program using a makefile peng12 Linux - Newbie 0 07-04-2015 02:44 PM
Compiling Issues:Compiling FFMPEG souce code on Visual Studio 2010 JAYANTHI Linux - Newbie 1 11-07-2013 10:41 PM
[SOLVED] Makefile for CUDA/C++ code naaman Programming 1 06-19-2010 11:00 AM
[SOLVED] Unsuccessful debug using Makefile during integration of cpp code to c code eryn Programming 4 05-13-2010 09:36 PM
Compiling old code: Makefile plus config.in cbcallaw Linux - Software 1 04-29-2004 05:35 PM

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

All times are GMT -5. The time now is 04:33 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