multiple definition of

Hello

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.


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "functions.h"
#include "functions.cpp"
using namespace std;



int main(int argc, char *argv[]) {

    cout << add(10, 5) << endl;

    return 0;
}


functions.h
1
2
int minimum (int a, int b);




functions.cpp
1
2
3
4
5
6
#include "functions.h"

int add (int a, int b) {
    return a + b;
}


Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#####################################################
# 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> 


The add function should be declared in the header file. You should never include source files (.cpp).

After I removed #include "functions.cpp"

it says.:
1
2
3
4
5
6
g++ -c main.cpp -o main.o -Wall -std=c++11
main.cpp: In function 'int main(int, char**)':
main.cpp:9:22: error: 'add' was not declared in this scope
     cout << add(10, 5) << endl;
                      ^
make: *** [main.o] Error 1
Hi,

Just some comments not related to your problem.

With your make file:

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.

Good Luck :+)

That doesnt help me. to get it compile. Just nicer error messages.
Are you still including functions.h in main.cpp?

Does functions.h contain the declaration of add()? The snipped you posted doesn't seem to include that.
Last edited on
Hi,

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 :+)

Regards
Last edited on
Topic archived. No new replies allowed.