1

NOTE: I made a DFH_lib.CPP where I included fstream and iomanip. I kept all the template functions in DFH_lib.CPP. Now, if I write the remaining NON-TEMPLATE functions in the MAIN.CPP and include DFH_lib.h only then it successfully runs. I don't understand why...

I was making a Data File Handling library using templates. I created two files:

DFH_lib.CPP
Lib_Test.CPP

I made a project and clicked on "Build All" under compile. I encountered the following linker error:

file_init(char near*) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP

AddColumn(const int near&) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP

file_init(char*); and AddColumn(T data, const int& width); and AddColumn(const int& width); are functions which I only defined in DFH_lib.CPP. I only made calls to these functions in Lib_Test.CPP.

DFH_lib.CPP

template <class T>    //Function belongs to Pretty Printing Libary
void AddColumn(T data, const int& width) {
    cout<<setw(width)<<data<<" | ";
}
void AddColumn(const int& width) {
    cout<<setw(width)<<setfill('_')<<"|";
}
void file_init(char* file) {   //File initialization function
    ofstream fout;
    fout.open(file, ios::binary|ios::noreplace);   //File Created, noreplace prevents data loss
    fout.close();
}

Lib_Test.CPP

cout<<endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13);
file_init(file);    //initializes the file

where "file" is defined as: char file[]="lib_Test.dat";

Could someone please explain why I'm getting this Linker Error? I don't understand what it means and therefore, how to fix it...

EDIT: I've noticed that this might be resulting due to a mistake done while including files, as I turned the Lib_Test.CPP into a "Hello World" program and the same error appeared. One more thing I noted: Only the non-template functions are causing the linking error!

DFH_lib.CPP

#ifndef _DFH_lib_cpp
#define _DFH_lib_cpp

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<string.h>
.....
#endif

Lib_Test.CPP

#include<iostream.h>
#include<conio.h>
#include"DFH_lib.CPP"  //Including DFH Libary
Community
  • 1
  • 1
  • you need to add the `#defines` to **each file** so also to `lib_Test.cpp` also the problem might be that the usage of template create a prototype that already exists. how is `char_member` and `int_member` defined? if they are the same it could be the reason ... How ever the include hierarchy looks good so it is either bug in template or in its usage which I do not see without complete source ... try to do MCVE (minimal compilable verifiable example ... in your case not compilable :)) – Spektre Aug 29 '17 at 14:29
  • Give me sometime, I got 3 exams this week – Ankur Singh Aug 29 '17 at 16:10
  • notify me after you made edits with comment with `@spektre` in it – Spektre Aug 29 '17 at 16:11

1 Answers1

0

I see following reasons for the problem:

  1. compiler error

    Old TC++ has sometimes compile problems with templates usually adding empty line (at the right place) or swap some lines of code help. But that usually starts only when your source code hit certain size like 30-50 Kbyte not sure anymore about the value it was ages ago. I am not convinced this is your issue.

  2. really a duplicate

    if you are including some file multiple times then it will lead to errors like yours. To remedy that you can encapsulate each file into this:

    #ifndef _DFH_lib_cpp
    #define _DFH_lib_cpp
    // here comes your file DFH_lib.cpp content
    #endif
    

    where _DFH_lib_cpp token is encoded filename you are encapsulating. This will throw away any duplicate includes. This also solves problem with global variables present but be careful they may not be the same across whole project if not included properly.

Spektre
  • 41,942
  • 8
  • 91
  • 312
  • I think It's integrated, I have options like build all, link, make, compile, etc. Also, the DFH_lib.CPP is 20KB and Lib_test.CPP is 4KB. I'll do some work regarding your second suggestion and get back to you if the problem persists. – Ankur Singh Aug 29 '17 at 09:35
  • @AnkurSingh yeah you're right just confirmed it in **DOS-BOX** `build all` is there too so **#3** is obsolete. I would first try the **#2** it is just 3 lines of code per each file.... heh have not run bc.exe for decades till now :) – Spektre Aug 29 '17 at 09:40
  • I did add those preprocessor commands in the DFH_lib.CPP but It didn't help. One thing I forgot to include is that, I also included the DFH_lib.CPP file directly into the lib_Test.CPP. I didn't use a header file cause that was giving me +7 errors. – Ankur Singh Aug 29 '17 at 10:26
  • @AnkurSingh those `#ifndef/define/endif` should be in each source file you got and encapsulating everything (includes and constant are inside). Your description hints you got wrong include and program structure hierarchy. To remedy that we need to see how your includes are done ... each file of your project (no code is needed just the includes and globals +/- headers). Just to be sure see [How approximation search works](https://stackoverflow.com/a/36163847/2521214) look for `_approx_h` each file should have unique `#define` so if you use filename as the token then you are fine. – Spektre Aug 29 '17 at 11:49
  • @AnkurSingh my bet is you are putting together more of foreign code into single project at once instead of incremental build up which would show you where the problem lies directly ... – Spektre Aug 29 '17 at 11:53
  • So, I just tried putting the prototypes of the non-template functions in a DFH_lib.h and then included the DFH_lib.h in the rest 2 .cpp files and the linker error is saying that DFH_lib.CPP's functions(the two mentioned above) are duplicated in the DFH_lib.CPP!!!!!! WHAT?! – Ankur Singh Aug 29 '17 at 14:14