2

My compiler is giving me some very strange errors about multiple definitions of a function (or rather all functions in a specific .c file).

I am using include guards to prevent multiple declarations

My header files contain NO definitions, only declarations. No variables are defined in the file, only functions.

I double-checked: Only the .h file is included in other files. The .c file isn't included anywhere.

I am using the same method to include the rest of my code and I have no problems there. Because of this, I can give no minimum example of my problem because I can see no (relevant) difference between the files that are working and the files that are causing the compiler-problems.

The error first started occuring after merging two branches in git. Each of the two branches is compiling without trouble before the merge.

For now, I would be glad about any hint at all about where this problem could come from.

I would also provide my code and/or error-logs if anybody needs them. It's a rather long code though, so please feel free to ask in case you need it

the error message from make is

error message

src/rhs.o: In function `calculate_rhs':
/home/user/Desktop/WS5/src/rhs.c:15: multiple definition of `calculate_rhs'
src/rhs.o:/home/user/Desktop/WS5/src/rhs.c:15: first defined here
src/rhs.o: In function `evaluate_rhs1':
/home/user/Desktop/WS5/src/rhs.c:103: multiple definition of `evaluate_rhs1'
src/rhs.o:/home/user/Desktop/WS5/src/rhs.c:103: first defined here
src/rhs.o: In function `evaluate_rhs2':
/home/user/Desktop/WS5/src/rhs.c:140: multiple definition of `evaluate_rhs2'
src/rhs.o:/home/user/Desktop/WS5/src/rhs.c:140: first defined here
collect2: error: ld returned 1 exit status
make: [exec] Error 1 (ignored)

compiler command for rhs.o is

/usr/lib/petscdir/3.6.0/arch-linux2-c-debug/bin/mpicc -o src/rhs.o -c -fPIC -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas -g3 -O0  -std=c99 -I/usr/lib/petscdir/3.6.0/include -I/usr/lib/petscdir/3.6.0/arch-linux2-c-debug/include    `pwd`/src/rhs.c

.h file is here: http://pastebin.com/Za37iWr7

.c file is here: http://pastebin.com/2mSzdZvT

The .h file is included only by the above .c file and the main-function

The headers of all other included (by rhs.c) files are:

Makefile: http://pastebin.com/LDj1e7xB

thanks in advance :)

multigrid101
  • 68
  • 1
  • 10
  • No, please construct an [minimal test-case](http://stackoverflow.com/help/mcve), and post exact error messages. Right now, we have nothing to go on. (Also, try doing a clean first, and then attempt to re-build.) – Oliver Charlesworth Jun 29 '15 at 12:14
  • another note: if I am interpreting the git diff correctly, then none of the changes that was introduced by the merge has anything to do with the file that is causing the problems..... I am out of ideas here, would be thankfull for any help. – multigrid101 Jun 29 '15 at 12:17
  • @amdixon: sorry it's on gitlab repo on a private server. Anyway I can upload the files here? – multigrid101 Jun 29 '15 at 12:20
  • @OliverCharlesworth: As mentioned above, I cannot post a minmal test-case because the minimal test cases actually work. I have no idea whats causing the problem and am mostly looking for any hint whatsoever right now. – multigrid101 Jun 29 '15 at 12:21
  • lets start with posting the exact error messages, and which compilation step is failing ( ie. makefile lines ). how many files ? – amdixon Jun 29 '15 at 12:23
  • @amdixon: It is only this one file. I edited the post to inlude the error message. The complete compiler output is rather long but I can only have two links in my question so I chose to link the .h and .c file. The step that is failing is the last compiliation step, i.e. linking of the .o files to get the executable. Hope this helps. – multigrid101 Jun 29 '15 at 12:35
  • added it in the post. – multigrid101 Jun 29 '15 at 12:43
  • The code you posted to pastebin looks fine. Maybe the error is introduced via `petscksp.h`? The errors look similar to when a non-static function is defined in a header file that is included more than once. – Toby Jun 29 '15 at 12:43
  • what happens if you remove `#include ` from the .c.. its already included in the .h – amdixon Jun 29 '15 at 12:44
  • removing `petscksp.h` doesn't help. – multigrid101 Jun 29 '15 at 12:47
  • pastebin the included .h files ( .c files not required ) – amdixon Jun 29 '15 at 12:56
  • I updated my question. But for some reason, everything was working fine before the merge I mentioned above. None of the files you requested seem to have changed due to that merge. – multigrid101 Jun 29 '15 at 13:18
  • also post petscksp.h – amdixon Jun 29 '15 at 13:20
  • The answer below provided a helpful hint. I will answer there and then go looking for that library header in a minute. – multigrid101 Jun 29 '15 at 13:23
  • Problem is solved. See below. Sorry for overlooking that error in the makefile.... – multigrid101 Jun 29 '15 at 13:28
  • the error messages posted are from the link activity, not the compile activity. The compile statement posted has nothing to do with the problem. – user3629249 Jun 30 '15 at 00:54

2 Answers2

1

Most likely your makefile or build scripts got messed up in the merge and it's linking the same file (rhs.o) twice.

Ionut
  • 6,436
  • 1
  • 17
  • 17
  • I posted my makefile above. I can't find an error there, but you seem to be right. – multigrid101 Jun 29 '15 at 13:24
  • JESUS how blind can a person be??? rhs appears twice in the makefile. I looked at that list over and over again and simply did not see it. Thanks for the hint. everything works fine now. Thanks also to the others who to tried to help. It turned out to be a pretty stupid mistake by me but it probably would have taken me hours to spot it without your help. – multigrid101 Jun 29 '15 at 13:27
0

the problem is in the make file.

Note: 'all' should be the first target, not the second

Note: every target that does not create a file with the target name should be included in a .PHONY: target

Note: macros should be defined with ':=' not '=' because the '=' will result in the macro being re-evaluated each time it is invoked while the ':=' will result in the macro only being evaluated once.

Note: write the include of the dependency files like so:

(expand the 'ifneq' to include 'clean_all' and 'cleaning' and 'clean_doc'

# wrap with ifneg... so will not rebuild *.d files when goal is 'clean_all', etc
#
ifneq "$(MAKECMDGOALS)" "clean_all"
-include $(OBJ:.o=.d)
-include $(TEST_OBJ:.o=.d)
endif

Here is the root of the problem:

OBJ = $(SRC)/main.o\
  $(SRC)/helper.o\
  $(SRC)/init.o\
  $(SRC)/basis.o\
  $(SRC)/gauss.o\
  $(SRC)/indices.o\
  $(SRC)/eval.o\
  $(SRC)/rhs.o\     <-- first instance of rhs.o
  $(SRC)/visual.o\
  $(SRC)/matrix.o\
  $(SRC)/rhs.o      <-- second instance of rhs.o

and this rule:

exec:  $(OBJ) chkopts
    -$(CLINKER) -o sim $(OBJ) $(PETSC_KSP_LIB)

contains an expansion of $(OBJ) where $(OBJ) contains the above error

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Thanks for the feedback. As mentioned above I already found the problem. But I will definitely have a look at your other suggestions as well. Btw: For some reason the `petsc` library (included in the makefile) changes a lot of make's setting anyway (e.g. default is changed to I don't know what). That's why I stopped bothering about the order of the targets. Thank for your tip regarding the `ifneq`, I was actually looking for something like that. – multigrid101 Jun 30 '15 at 16:34