Linker Error: Duplicated Functions

北城余情 提交于 2020-01-05 03:36:56

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/45921184/linker-error-duplicated-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!