Jump to content
  • Advertisement
Sign in to follow this  
Fibonacci One

Makefile help

This topic is 5404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I'm working on a simple program in C and I figured I may as well go ahead and use this to learn how to use makefiles. Now, I can get single file programs to work just fine, but now I'm trying to make a program that draws from two files.
the directory structure looks like this:
Project
--Includes
  -BinaryTree.h
--SRC
  -BinaryTree.c
  -Project.c
-Makefile

As you have probably already assumed, Project.c includes BinaryTree.h with includes BinaryTree.c and the makefile I'm trying to use (I took out all of the bash stuff, but it's still the same thing):
all:	Project.o BinaryTree.o
		ld Project.o BinaryTree.o
Project.o:	BinaryTree.o
		gcc -g SRC/Project.c 
BinaryTree.o:	
		gcc -g SRC/BinaryTree.c 


When I run make, I get all sorts of "Multiple Definition of _something" errors. I'm guessing this means I'm linking them incorrectly because I know I never created any _somethings.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
your specific problem is that you must call gcc with -c for the two sub-files. -c means "just make an object file"

then, it is probably better to call gcc instead of ld. i think that's the general usage.

with those two changes, your makefile would look like:

all: Project.o BinaryTree.o
gcc -g Project.o BinaryTree.o

Project.o: BinaryTree.o
gcc -g -c SRC/Project.c
BinaryTree.o:
gcc -g -c SRC/BinaryTree.c

there are two interesting things here:

1) your executable will be called a.out, i believe
2) you will always be relinking your exe, because you don't have its name in the dependency list. so you might want to change the top of your makefile to do:

all: project

project: Project.o BinaryTree.o
gcc -g Project.o BinaryTree.o -o project

furthermore, you will want to look into default "suffix rules" so that you can figure out how to get make to create your two ".o" rules for you.. instead of calling out every file separately.

hope that helps.

Share this post


Link to post
Share on other sites
Also, it is probably a bad idea to be including the BinaryTree.c file from the .h. The .h is supposed to contain everything that Project.c needs to know about BinaryTrees (i.e., interface, not implementation), and the other stuff doesn't need to be included yet. It gets compiled in the BinaryTree.o, and reunited when you do the linking. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!