• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 892
  • Last Modified:

C++ Second Defintion Ignored Warnings... struct objects header & CPP

I am unable to link my project without "second definition ignored' warnings.
1>__NDatabase.obj : warning LNK4006: "struct _DataTypeMap * DataTypeMap" (?DataTypeMap@@3PAU_DataTypeMap@@A) already defined in __NVTransfers.obj; second definition ignored

Are my header and CPP files correctly configure for a object type of struct ?

Header file
#pragma once
#include "StaticData.h"

#ifndef _DataTypeMap_IMPLEMENT_
#define _DataTypeMap_IMPLEMENT_

struct _DataTypeMap
	UINT    OrdinalType;
	LPCTSTR SQLInteger;           // SQL_INTEGER  = 4
	LPCTSTR SQLSmallInt;          // SQL_SMALLINT = 5
	LPCTSTR SQLTinyInt;           // SQL_TINYINT  = -6
	LPCTSTR SQLBigInt;            // SQL_BIGINT   = -5
	LPCTSTR SQLReal;              // SQL_REAL     = 7
	LPCTSTR SQLNumeric;           // SQL_NUMERIC  = 2
	LPCTSTR SQLDecimal;           // SQL_DECIMAL  = 3
	LPCTSTR SQLFloat;             // SQL_FLOAT    = 6
	LPCTSTR SQLDouble;            // SQL_DOUBLE   = 8
	LPCTSTR SQLChar;              // SQL_CHAR     = 1
	LPCTSTR SQLVarChar;           // SQL_VARCHAR  = 12
	LPCTSTR SQLLongVarChar;       // SQL_LONGVARCHAR = -1
	LPCTSTR SQLDateTime;          // SQL_DATETIME = 9
	LPCTSTR SQLDate;              // SQL_DATE     = 9, SQL_TYPE_DATE = 91
	LPCTSTR SQLTime;              // SQL_TIME     = 10, SQL_TYPE_TIME = 92
	LPCTSTR SQLBit;               // SQL_BIT      = -7
	LPCTSTR SQLVarBinary;         // SQL_VARBINARY = -3
	LPCTSTR SQLNull;              // 
} DataTypeMap[] = {
	_T("Default"),                 SQLSRV, _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Oracle"),                  ORACLE,     _T("integer"), _T("integer"),  _T("integer"), _T("number"), _T("float"), _T("numeric"), _T("number"), _T("float"), _T("number"), _T("longvarbinary"), _T("char"), _T("varchar2"), _T("longvarchar"), _T("timestamp"), _T("date"), _T("date"),     _T("time"), _T("bit"), _T("raw"),        _T("null"),
	_T("Ingres"),                  OPEN_INGRES,     _T("integer"), _T("integer"),  _T("integer"), _T("number"), _T("float"), _T("numeric"), _T("number"), _T("float"), _T("number"), _T("longvarbinary"), _T("char"), _T("varchar"), _T("longvarchar"), _T("timestamp"), _T("date"), _T("date"),     _T("time"), _T("bit"), _T("raw"),        _T("null"),
	_T("Oracle8"),                 ORACLE,     _T("integer"), _T("integer"),  _T("integer"), _T("number"), _T("float"), _T("numeric"), _T("number"), _T("float"), _T("number"), _T("longvarbinary"), _T("char"), _T("varchar2"), _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("raw"),        _T("null"),
	_T("Microsoft SQL Server"),    SQLSRV, _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("image"),         _T("char"), _T("varchar"),  _T("longvarchar"), _T("datetime"),  _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Rdb"),                     RDB,        _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Access"),                  ACCESS,     _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Informix"),                INFORMIX,   _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Sybase"),                  SYBASE,     _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("datetime"), _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("DB2"),                     DB2,        _T("integer"), _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("MySQL"),                   MYSQL,      _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double"),           _T("longblob"),      _T("char"), _T("varchar"),  _T("longtext"),    _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("tinyint(1)"), _T("varbinary"), _T("null"),
	_T("TurboImage"),              TURBOIMAGE, _T("integer"), _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("numeric"), _T("float"), _T("double"),           _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Attunity Connect Driver"), ATTUNITY,   _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),
	_T("Rename Me"),               CONNX,      _T("int"),     _T("smallint"), _T("tinyint"), _T("bigint"), _T("real"), _T("numeric"), _T("decimal"), _T("float"), _T("double precision"), _T("longvarbinary"), _T("char"), _T("varchar"),  _T("longvarchar"), _T("timestamp"), _T("datetime"), _T("date"),     _T("time"), _T("bit"), _T("varbinary"),        _T("null"),

Open in new window

CPP  File
#include "stdafx.h"
#include "_DataTypeMap.h"

Open in new window

2 Solutions
"second definition ignored' warning means: "struct _DataTypeMap * DataTypeMap" is
compiled into  __NDatabase.obj and into __NVTransfers.obj, but only one object with
the same (mangled) name  (i.e. same name and same prototype) can be linked into
your executable.

Header files should be used for definitions, but not for actual variables, as they tend to be
included multiple times.

To get rid of your problem
// in header file: the prototype
// can be included multiple times
struct _t_DataTypeMap
typedef struct _t_DataTypeMap _DataTypeMap;

extern _DataTypeMap DataTypeMap[];

in ONE c++ file (e.g. main.cpp)
_DataTypeMap DataTypeMap[] = {
	_T("Default"),                 SQLSRV, _T("int"),     

Open in new window

if you have a manager class (singleton class) which handles the DataTypeMap i would suggest to make the definition of the array a member of this class. this class also could provide functions to access the array from other classes if needed.

generally, you can't put a definition into a header file cause each cpp which includes the header would create the same symbol (a named global variable pointing to some part of memory) again what gives linker error. header files only may contain declarations, for example of struct/class or function prototypes, extern declarations as told by DonConsolio, preprocessor macros enumerations, and inline code.

TimPeerAuthor Commented:
I abandoned the struct type and embedded in a class with the CMapStringToOb so will split the points since I didn't select either solution.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now