# Make-File Help

## Recommended Posts

Okay, I'm trying to figure out how to write a makefile. I first found a long and awful tutorial on how to write one from scratch... Then I got directed to simply make, automake, configure, autoconf. Then to somehow getting configure.ac files... I'm getting super lost, I am not getting anywhere on google. I even thought I found a way for Emacs to do it for me... Can anyone give me a good place to start? Tutorials? Advice? (Btw: I am using the make file with Intel's C++ Linux compiler, I am using Emacs to code in.)

##### Share on other sites
The manual for GNU make has a lot of explanation. It's not tutorial, but it does explain the concepts and has lots of examples. Here is a copy in HTML form: GNU Make

The core concept of make is pretty simple. A makefile consists of a list of rules which form a tree of files linked by dependencies. If any file in the tree is missing or is older than any of its children, a command is executed (which usually rebuilds the file from its children). The most important thing to understand is that make is not designed to execute a sequence of commands like a batch file. The commands that are executed and the order in which they are executed are completely determined by the dependency tree.

##### Share on other sites
Well I checked up on that, and I got somewhere.

I am trying to use a makeFile to compile the one line "Hello World" app.

This is my make file:
#(tabs before system commands, yes I know)
# lab1.make -- this is a comment line, ignored by make utility
all: Hello
lab1.out : Hello.o
icc -no-ipo -o lab1.out Hello.o
# above, we are saying lab1.out depends on main1.o, mylib.o and openfile.o
# and to create lab1.out we give the g++ command as shown on the next line

# which starts with a TAB although you cannot see that .
# note that the command : g++ -o lab1.out main1.o mylib.o openfile.o
# creates an executable file named lab1.out from the 3 object files respectively.
Hello.o: Hello.cpp
icc -c Hello.cpp
clean:
rm *.o
# above we are stating how to run the rule for clean, no dependencies,
# what we want is when we ask to do a "make -f lab1.make clean"
# that will not do anything except remove executable and object files
# so we can "clean out" our directory of unneeded large files.
# we only do a make clean when we want to clean up the files.

# END OF MAKE FILE

These are my errors:
make: *** No rule to make target hello', needed by all'. Stop.
halsafar@localhost:/share/Cpp/HelloWorld$make -f MakeFile icc -c Hello.cpp cc Hello.o -o Hello Hello.o(.text+0x19): In function main': Hello.cpp: undefined reference to __intel_proc_init' Hello.o(.text+0x1e):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x25):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x35):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x4c):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x58):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x61):Hello.cpp: more undefined references to std::cout' follow Hello.o(.text+0xe0): In function main': Hello.cpp: undefined reference to std::ios_base::clear(std::_Iosb<int>::_Iostate, bool)' Hello.o(.text+0xeb):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0xf4):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x112):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x11b):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x135):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x13c):Hello.cpp: more undefined references to std::cout' follow Hello.o(.text+0x24f): In function main': Hello.cpp: undefined reference to std::ios_base::clear(std::_Iosb<int>::_Iostate, bool)' Hello.o(.text+0x25d):Hello.cpp: undefined reference to std::uncaught_exception()' Hello.o(.text+0x2b1):Hello.cpp: undefined reference to std::ios_base::clear(std::_Iosb<int>::_Iostate, bool)' Hello.o(.text+0x300):Hello.cpp: undefined reference to __cxa_begin_catch' Hello.o(.text+0x321):Hello.cpp: undefined reference to std::ios_base::clear(std::_Iosb<int>::_Iostate, bool)' Hello.o(.text+0x329):Hello.cpp: undefined reference to __cxa_end_catch' Hello.o(.text+0x333):Hello.cpp: undefined reference to __cxa_end_catch' Hello.o(.text+0x395):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x3ae):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x3d2):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x3e3):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x3fa):Hello.cpp: undefined reference to std::cout' Hello.o(.text+0x4b6): In function __sti__$E':
Hello.cpp: undefined reference to std::ios_base::Init::Init()'
Hello.o(.text+0x4c5):Hello.cpp: undefined reference to std::ios_base::Init::~Init()'
Hello.o(.text+0x4d4):Hello.cpp: undefined reference to std::_Winit::_Winit()'
Hello.o(.text+0x4e3):Hello.cpp: undefined reference to std::_Winit::~_Winit()'
Hello.o(.gnu.linkonce.t._ZNSo6sentryD1Ev[.gnu.linkonce.t._ZNSo6sentryD1Ev]+0x1a): In function std::basic_ostream<char, std::char_traits<char> >::sentry::~sentry()':
Hello.cpp: undefined reference to std::uncaught_exception()'
Hello.o(.gnu.linkonce.t._ZNSo6sentryD1Ev[.gnu.linkonce.t._ZNSo6sentryD1Ev]+0x72):Hello.cpp: undefined reference to std::ios_base::clear(std::_Iosb<int>::_Iostate, bool)'
Hello.o(.eh_frame+0x12): undefined reference to __gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [Hello] Error 1

Hello.cpp compiles fine if I:
icc Hello.cpp

##### Share on other sites
Try replacing lab1.out with hello.

##### Share on other sites
SHELL = /bin/sh
CC = g++
OBJECTS = hello.o

all : hello

hello : $(OBJECTS)$(CC) $(OBJECTS) -o$@

hello.o :
$(CC) -c hello.cpp #### Share this post ##### Link to post ##### Share on other sites Here is a pretty good tutorial on writing makefiles. http://www.eng.hawaii.edu/Tutor/Make/ #### Share this post ##### Link to post ##### Share on other sites Most of your errors don't seem to be to do with your makefile as such, rather due to not passing in the correct libraries to gcc. I.e., if you use the same commands to compile and link manually, the errors will probably still appear. #### Share this post ##### Link to post ##### Share on other sites In a lot of cases, it is easy to go too far with a Makefile. If I had a file called bingo.cpp, I could just type: make bingo and it would compile fine, even without a Makefile present. If it depended on routines in bingonumber.cpp, I would design a Makefile like this: bingo: bingonumber.o Now I can type "make bingo" and it will automatically compile bingonumber.o first, and then link it into bingo. Here's an example of the Makefile I use in my examples directory: CPPFLAGS += -Wno-deprecated-declarationsBINS = openal openil function_pointer inheritance_test precisionOPENAL = -lopenal -lalutOPENIL = -lIL -lILU -lILUTOPENGL = -lGL -lGLU -lglutLIBXML = -I/usr/local/include/libxml2 -L/usr/local/lib -lxml2 -lzall:${BINS}openal: LDFLAGS += ${OPENAL}openil: LDFLAGS +=${OPENGL} ${OPENIL}xml: CPPFLAGS +=${LIBXML}xml2: CPPFLAGS += ${LIBXML}clean: rm${BINS}

Note that if I just type "make", it will make everything, because "all" is the first entry. I can still, however just type "make xml2" and it will make only that. This doesn't show a good example of dependencies, however.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627676
• Total Posts
2978582

• 11
• 12
• 10
• 12
• 22