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:
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:
- I define EVERY function inside the corresponding .cc file.
- 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.