I need help getting my code compile using make file. I need to have both .h and .cpp files. And I need to write make file for compiling it. When I open my code in IDE and hit F5, it runs fine, but not when I use make file.
#####################################################
# You may need to change the parameters under here. #
#####################################################
# Step 1: Choose a compiler. By default, use clang++
# If you use clang++, make sure the following line does not start with a comment (#)
CXX=g++
DOXYGEN = doxygen
# If you use g++, uncomment the following line
#CXX=g++
# Set default compiler parameters
# -Wall shows all warnings when compiling, always use this!
# -std=c++11 enables the C++11 standard mode
CXXFLAGS = -Wall -std=c++11
# Step 2: If you use clang++ under Mac OS X, remove these comments
#CXXFLAGS += -stdlib=libc++
#LFLAGS += -stdlib=libc++
# Step 3: Run 'make' in the same directory as this file
########################################################
# You need to make changes below here to solve Task 2. #
########################################################
all: build doc
# Declare the name of our program (in Windows, the compiler adds .exe)
PROGRAM = program
# The needed object files (we make these from the source code)
OBJ = main.o functions.o
# This is the first target. It will be built when you run 'make' or 'make build'
build: $(PROGRAM)
# Rule for linking IMPORTANT! The space in front of $(CXX) is a TABULATOR!
$(PROGRAM): $(OBJ)
$(CXX) $(OBJ) $(LFLAGS) -o $(PROGRAM)
# Rules for compiling
main.o: main.cpp
$(CXX) -c main.cpp -o main.o $(CXXFLAGS)
functions.o: functions.cpp
$(CXX) -c functions.cpp -o functions.o $(CXXFLAGS)
clean:
rm -f *.o
rm -fr ./doc
Command line output:
1 2 3 4 5 6 7 8 9 10
D:\Dropbox\cpp\test>make
g++ -c main.cpp -o main.o -Wall -std=c++11
g++ -c functions.cpp -o functions.o -Wall -std=c++11
g++ main.o functions.o -o program
functions.o:functions.cpp:(.text+0x0): multiple definition of `add(int, int)'
main.o:main.cpp:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
make: *** [program] Error 1
D:\Dropbox\cpp\test>
If you have clang++, then use it - it has nice error messages. I gather that it is the default with XCode. If you don't have it, consider downloading it and installing.
With:
CXXFLAGS = -Wall -std=c++11
Use these options as well, as a minimum each time:
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic
You should be able to compile with c++14 if your compiler is up to date. -Wextra -pedantic turn on options that aren't turned on with -Wall. If you read the compiler manual there are other options that might be worthwhile as well, I know there are a zillion lines, but it is worth it if you have time.
That doesnt help me. to get it compile. Just nicer error messages.
TheIdeasMan wrote:
Just some comments not related to your problem.
Peter87 had already given some advice on how to fix your problem, and MikeyBoy is on the case too, so you should be pretty right with those two very experienced guys :+)