LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-06-2005, 06:28 AM   #1
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
C programming: Adding new source file to project


I have the source for a large c programming project which I'm going to modify. Naturally, the source comes with a Makefile. However, how do I add new source and header files to the project? I guess the Makefile must be modified, but is there tools out there for automatic modification of the Makefile? It seems rather inefficient having to modify the Makefile manually every time I add a new file to the project.
 
Old 11-06-2005, 06:50 AM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Read about autotools online http://sources.redhat.com/autobook/a...l#SEC_Contents or download a copy http://sources.redhat.com/autobook/autobook-1.4.tar.gz
 
Old 11-06-2005, 07:54 AM   #3
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
I took a glance at it, and it seems like a full days work just reading it. Could you give me an hint on which sections to read - I'm guessing not all of the sections are relevant for my project. I allready have a makefile, and just need to find a smart way of including new source files.
 
Old 11-06-2005, 11:37 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
The following is a (modified) example from John Goerzen's The Linux progrmming Bible.
Code:
# Lines starting with the pound sign are comments.
#
# These are the two options that may need tweaking

EXECUTABLE = scosch
LINKCC = $(CC)

# You can modify the below as well, but probably
# won't need to.
#

# CC is for the name of the C compiler. CPPFLAGS denotes pre-processor
# flags, such as -I options. CFLAGS denotes flags for the C compiler.
# CXXFLAGS denotes flags for the C++ compiler. You may add additional
# settings here, such as PFLAGS, if you are using other languages such
# as Pascal.

CPPFLAGS = 

LDFLAGS = -lpthread -lm

CC = gcc
#CFLAGS = -Wall -O2 -ggdb3	# debugging
CFLAGS = -Wall -O2		# normal 

CXX = g++
CXXFLAGS = $(CFLAGS)


SRCS := $(wildcard *.c) $(wildcard *.cc) $(wildcard *.C)
OBJS := $(patsubst %.c,%.o,$(wildcard *.c)) \
        $(patsubst %.cc,%.o,$(wildcard *.cc)) \
        $(patsubst %.C,%.o,$(wildcard *.C))
DEPS := $(patsubst %.o,%.d,$(OBJS))

# "all" is the default target. Simply make it point to myprogram.

all: $(EXECUTABLE)

# Define the components of the program, and how to link them together.
# These components are defined as dependencies; that is, they must be
# made up-to-date before the code is linked.

$(EXECUTABLE): $(DEPS) $(OBJS)
	$(LINKCC) $(LDFLAGS) -o $(EXECUTABLE) $(OBJS)

# Specify that the dependency files depend on the C source files.

%.d: %.c
	$(CC) -M $(CPPFLAGS) $< > $@
	$(CC) -M $(CPPFLAGS) $< | sed s/\\.o/.d/ >> $@

%.d: %.cc
	$(CXX) -M $(CPPFLAGS) $< > $@
	$(CXX) -M $(CPPFLAGS) $< | sed s/\\.o/.d/ >> $@

%.d: %.C
	$(CXX) -M $(CPPFLAGS) $< > $@
	$(CXX) -M $(CPPFLAGS) $< | sed s/\\.o/.d/ >> $@

# Specify that all .o files depend on .c files, and indicate how
# the .c files are converted (compiled) to the .o files.

clean:
	-rm $(OBJS) $(DEPS) *~
cleanall:
	-rm $(OBJS) $(EXECUTABLE) $(DEPS) *~

tar:
	tar -cf scosch.tar *.c *.h *.cfg Makefile
	@echo "scosch.tar created"

explain:
	@echo "The following information represents your program:"
	@echo "Final executable name: $(EXECUTABLE)"
	@echo "Source files:     $(SRCS)"
	@echo "Object files:     $(OBJS)"
	@echo "Dependency files: $(DEPS)"

depend: $(DEPS)
	@echo "Dependencies are now up-to-date."

-include $(DEPS)
It automatically picks up new C-files and creates/checks dependencies. It will work as long as all sourcefiles are in one directory.
The book contains examples as well if you have a project which is split over subdirectories.

Modify the flags and the executable name to your requirements. I've added the tar and might have changed something more (long ago). I'm using it now for approx four years and does what I need it to do.
 
Old 11-07-2005, 10:17 AM   #5
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Thanks for the script.


However, this is the case: The project I'm working on contains one makefile per subdirectory. Since I'm modifyin the code in just one of these, I
guess I only need to be concerned with the makefile in that particular directory. I was told that the only thing I needed to do when adding a new source file was to edit the file Makefile.am and the run make. So I did. Not surprisingly, it didn't work. Why is this so? I was told that when make was run, the script in Makefile.am added the new files automatically. What am I doing wrong?

If needed I can paste the contents of Makefile.am.

EDIT: It seems as the syntax is right, that is, the function defined in the new source file is recognized. The error seems to occur when the actual building takes place, not while checking the syntax:
Code:
  ./.libs/libospf.so: undefined reference to `wospf_hello'
But again, how do I solve this?

Last edited by kenneho; 11-07-2005 at 10:34 AM.
 
Old 11-07-2005, 10:20 PM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Sorry, I'm not a makefile specialist (that is why I gave the example without any explanantion)..

I guess that you now have multiple makefiles (one per subdirectory) and one main makefile. So first question is where did you add the source (subdirectory or maindirectory) and which makefile did you modify?
 
Old 11-07-2005, 11:41 PM   #7
sasho
Member
 
Registered: Jan 2005
Distribution: Arch
Posts: 120

Rep: Reputation: 17
are you multiple make files generated with a configure script?
 
Old 11-08-2005, 02:26 AM   #8
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
I figured it out!

I have one makefile per subdirectory, and they were all created by the configuration-script. I'm not sure exactly which of the following
things that made it work: Looking more closely at the make-output I noticed that the script complained about my automake-version. Therefore I
installed version 1.9, as version 1.4 was the version currently installed on my system. I addition I run aclocal, as was suggested somewhere for updating the Makefile.am or something. Anyways, this did the trick, and it now works.
 
Old 11-08-2005, 09:09 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
not much help but worth considering in the future?

recursive make considered harmful
http://www.pcug.org.au/~millerp/rmch...cons-harm.html
 
  


Reply

Tags
makefile, programming



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
open source C,C++ game project WindowsBurner Linux - Games 166 09-28-2006 02:37 PM
I want to do a open source project coolguy_iiit Programming 11 04-29-2005 03:47 PM
portable programming - any suggestions for kicking off a project? PBSchmidt Programming 2 09-03-2004 02:57 PM
need help for grad project in programming corbis_demon Programming 1 07-11-2004 03:36 PM
Help me find an open-source project Creep Programming 4 11-16-2003 10:05 PM

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

All times are GMT -5. The time now is 08:08 AM.

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