0

I know this is a common and frequent question. I'm working on visual-studio-code and using code-runner extension for convenience. My project working tree is the following:

enter image description here

I'm trying to compile serial.cc (the main) using the following code-runner setting taking a cue from what is stated here:

"cpp": "cd $dir && g++ -Wall -fopenmp $fileName utilities/*.cc -o $fileNameWithoutExt.o && $dir$fileNameWithoutExt.o"

Here's the following files includes:

  • serial.cc #include "utilities/CSVIterator.h"
  • CSVIterator.h #include "CSVRow.h"
  • CSVRow.h #include "utility.h"

What I get is a 'multiple definition' error triggered by CSVRow.cc and utility.cc listing a series of functions (namely every function defined inline -not actually using inline keyword- inside utility.h) that the compiler sees already defined into serial.cc . E.g. inside utility.h I have:

double cpuSecond()
{
    struct timeval tp;
    gettimeofday(&tp, NULL);
    return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6);
}

Problem is fixed if:

  1. I define EVERY function inside the corresponding .cc file.
  2. I move EVERY function inside the corresponding .h file and so removing every source file apart from the main one (serial.cc).

I understand this is too little to understand so here's the full GitHub Repo. Thanks in advance.

Barnercart
  • 1,523
  • 1
  • 11
  • 23
  • How are these functions defined? You say "namely every inline defined function of CSVRow.h and utility.h", but are they actually `inline`? – Kevin Feb 27 '20 at 21:37
  • Do you use include guards like `#pragma once`? – alain Feb 27 '20 at 21:40
  • Just updated the question. Not actually inline anyway. – Barnercart Feb 27 '20 at 21:41
  • There are declarations and definitions. Usually, the declaration goes into the .h and the definition into the .cpp. You can never have two definitions. What you wrote reads like you're using definitions in both .h and .cpp. Please post your code (a minimal example of it). – alain Feb 27 '20 at 21:49
  • Updated question with GitHub repo. I understand the informations I gave you were too little. – Barnercart Feb 27 '20 at 21:52
  • 1
    You say you defined your function `cpuSecond` in `utility.h`. This is **wrong.** Simplifying, you should *never* have a body of a function inside header file, unless you meet certain conditions (the function is `inline`). Move all the function definitions from header files to source files. – Yksisarvinen Feb 27 '20 at 21:53
  • I think it does @Yksisarvinen . So even using 'inline' is not that great? – Barnercart Feb 27 '20 at 21:58
  • 1
    @Barnercart Making all functions defined in header `inline` is going to fix the issue. Whether it's a good practice or not - debatable. Many C++ programmers simply prefer the practice of separating declarations and definitions to keep code tidy (IMO a class with separate header is much easier to read than e.g. Java class). Using too much `inline` may increase compilation time, as compiler has to compile same function several times, but you likely won't notice the difference until you have hundreds of those. – Yksisarvinen Feb 27 '20 at 22:03

0 Answers0