简体   繁体   中英

C++ Compiles in Windows, multiple definition error in Linux

let me start by saying that I am not the best C++ and I know very little in ways of Linux. For a class project we had to implement a heap so I coded everything on my Windows PC assuming I could just upload the files to the Linux repository the school has. [Perhaps this is where I went wrong and this cannot be done this simply.] My codes compiles and clears all test cases provided on my Windows pc. When I upload the files to Linux, I created a makefile and when I use the make command I get back a laundry list of multiple definition errors. One error per function that I am using. I have done some searching and I am more confused then when I started.

My files are: main.cpp, main.h, heap.cpp, heap.h, util.cpp, and util.h.

I think the issue is with my include statements but I am not 100% sure.

Here is an example of the files.

main.cpp

 #include <iostream>  //needed to use basic inputs/outputs
 #include <stdio.h>
 #include <stdlib.h>
 #include "main.h"
 #include "util.cpp"
 #include "heap.cpp"
 #include <fstream>
 using namespace std;

main.h is blank.

heap.cpp

 #include <iostream>  //needed to use basic inputs/outputs
 #include <stdio.h>
 #include <stdlib.h>
 #include "heap.h"
 #include <cmath>
 #include <math.h>
 using namespace std;

 //expanded functions found in the heap.h file

heap.h

 //2 structs
 //9 functions

util.cpp

 #include <iostream>  //needed to use basic inputs/outputs
 #include <stdio.h>
 #include <stdlib.h>
 #include "util.h"
 using namespace std;

 //expanded functions found in util.h

util.h

 //1 function

Between heap.h and util.h I have 10 function and upon running the make command I get a warning about all ten:

 multiple definition of 'FUNCTIONNAME'
 main.o:main.cpp:(.text+0x1b7): first defined here

I am assuming the 0x1b7 is a memory location because they are each different.

Any help would be greatly appreciated.

You haven't shown the Makefile, but most likely, it includes this or a similar rule

program: main.o heap.o util.o
    $(CXX) $(CXXFLAGS) -o program main.o heap.o util.o

What happens now, the compiler builds the three object files main.o, heap.o and util.o. Next the object files are linked together to build program .

The linker sees definitions of the various functions defined in both main.o and heap.o, or main.o and util.o respectively. This is why it complains about "multiple definition of 'FUNCTIONNAME'"


Why are these functions defined more than once?

When you include a file into another source, it is as if you copy the contents at the location of the #include . This means a function defined in heap.cpp:

void create_heap(int size)
{
    // create a heap ...
}

is copied verbatim into main.cpp where the line

#include "heap.cpp"

is.


Because heap.cpp has the definition of create_heap() and main.cpp #include s the contents of heap.cpp, both contain their own copy of create_heap() . Now you compile both heap.o and main.o, and link them together. Each object file has a copy of create_heap() and this is where the linker is confused and complains

multiple definition of 'create_heap'
main.o:main.cpp:(.text+0x1b7): first defined here

To fix this, simply replace the lines including the cpp sources, eg

#include "util.cpp"
#include "heap.cpp"

with their respective header files

#include "util.h"
#include "heap.h"

Only keep the function definitions relevant to main.cpp, nothing else. Now main.cpp has no function definitions belonging to util.cpp or heap.cpp, and the linker errors are gone.


Presumably, this worked on Windows, because only main.cpp was included in the project file, and so only one definition (from main.o) was in the resulting executable.

If you had included all sources as in the Linux Makefile, the error could be seen in Windows as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM