Search code examples
c++visual-studiovisual-studio-2013linkerlnk2005

Why do I get so many `LNK2005` errors despite using `#ifndef #define #endif` code blocks in my .h files?


I have 21 files as shown here in the picture:

enter image description here

The following 35 functions are declared in lines 364-411 of util.h inside a

#ifndef FlagUtil

#define FlagUtil

#endif  

code block:

Create_Pix, Remove_Pix, my_round, edit_error, check_file, check_dir, 
read_config, write_config, write_envi_config, my_randomize, my_random, 
my_eps_random, cadd, csub, cmul, cdiv, cpwr, cconj, cimg, crel, cmod, cmod2, 
angle, cplx_sinc, PolTypeConfig, init_file_name, memory_alloc, PrintfLine, 
CreateUsageHelpDataFormat, CreateUsageHelpDataFormatInput, 
init_matrix_block, block_alloc, CheckFreeMemory, CheckFreeMemoryWin32, 
CheckFreeMemoryLinux  

The following 30 functions are declared in lines 99-135 of util_block.h inside a

#ifndef FlagUtilBlock

#define FlagUtilBlock

#endif  

code block:

read_matrix_int, read_matrix_float, read_matrix_cmplx, write_matrix_int, 
write_matrix_float, write_matrix_cmplx, read_matrix3d_float, 
read_matrix3d_cmplx, write_matrix3d_float, write_matrix3d_cmplx, 
read_block_matrix_int, read_block_matrix_float, 
read_block_matrix_matrix3d_float, read_block_matrix_cmplx, 
write_block_matrix_int, write_block_matrix_float, 
write_block_matrix_matrix3d_float, write_block_matrix_cmplx, 
write_block_matrix3d_float, write_block_matrix3d_cmplx, read_block_S2_avg, 
read_block_S2_noavg, read_block_S2T6_avg, read_block_SPP_avg, 
read_block_SPP_noavg, read_block_TCI_avg, read_block_TCI_noavg, 
read_block_S2_TCIelt_noavg, read_block_SPP_TCIelt_noavg, average_TCI  

The following 35 functions are declared in lines 106-149 of util_convert.h inside a

#ifndef FlagUtilConvert

#define FlagUtilConvert

#endif  

code block:

S2_to_C3elt, S2_to_C4elt, S2_to_T3elt, S2_to_T4elt, S2_to_T6elt, 
SPP_to_C2elt, SPP_to_T2elt, S2_to_SPP, S2_to_IPP, S2_to_C2, S2_to_C3, 
S2_to_C4, S2_to_T2, S2_to_T3, S2_to_T4, S2_to_T6, SPP_to_C2, SPP_to_T2, 
SPP_to_IPP, SPP_to_T4, C2_to_IPP, C2_to_T2, T2_to_C2, C4_to_T4, C4_to_C3, 
C4_to_T3, C4_to_C2, C4_to_IPP, T4_to_C4, T4_to_C3, T4_to_T3, C3_to_T3, 
C3_to_C2, C3_to_IPP, T3_to_C3, T6_to_C3  

And the contents of the file PolSARproLib.h is:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif

/* ROUTINES DECLARATION */
#include "util.h"
#include "util_block.h"
#include "util_convert.h"
#include "graphics.h"
#include "matrix.h"
#include "processing.h"
#include "statistics.h"
#include "sub_aperture.h"
#include "my_utils.h"  

And the contents of the file PolSARproLib.c is:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>

#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif

/* ROUTINES DECLARATION */
#include "util.c"
#include "util_block.c"
#include "util_convert.c"
#include "graphics.c"
#include "matrix.c"
#include "processing.c"
#include "statistics.c"
#include "sub_aperture.c"
#include "my_utils.c"  

I’ve placed those 21 files in a folder named Static Project and I’ve created the PolSARproLib.lib file as follows, my machine is a Win 10x64 one: (here I’ve uploaded this project):

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

The files graphics.obj, matrix.obj, my_utils.obj, PolSARproLib.lib, PolSARproLib.obj, processing.obj, statistics.obj, sub_aperture.obj, util.obj, util_block.obj, util_convert.obj are created in the Debug folder of the project without any error, but some warnings like:

1>graphics.obj : warning LNK4006: _write_header_bmp_8bit already defined in 
PolSARproLib.obj; second definition ignored  

enter image description here

enter image description here

I have created a folder named Arii and placed the following 22 files in the subfolder \Arii\lib

enter image description here

I have also the file arii_anned_3components_decomposition.c placed in the folder Arii:

enter image description here

Now I create the project arii_anned_3components_decomposition in the folder Arii as follows (here I’ve uploaded the project)

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

In the following 2 pictures, you see the configuration of the project. Again, I should say that I’m working on a Win 10x64 machine

enter image description here

enter image description here

External library is added to the project as follows:

Properties>C/C++>General>Additional Include directories

I enter the path to the folder in which header files .h are placed

enter image description here

Properties>Linker>General>Additional library directories  

I enter the path to the folder in which .lib files are placed

enter image description here

In

Properties>Linker>Input>Additional Dependencies

I’ve entered name of those needed .lib files:

enter image description here

Here is the beginning (before main function) of the file arii_anned_3components_decomposition.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "omp.h"

#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif

/* ROUTINES DECLARATION */
#include "../lib/PolSARproLib.h"  

Now I build the solution:

enter image description here

And unfortunately I get 104 errors. 100 of them is about multiple definition of the functions in util.h, util_convert.h, util_block.h (those functions are listed in the beginning of question)

Errors 1-100 are alike:

1>PolSARproLib.lib(PolSARproLib.obj) : error LNK2005: _C3_to_T3 already 
defined in PolSARproLib.lib(util_convert.obj)    

enter image description here

Here I’ve uploaded the error and warning file.txt

  • I really don’t understand why am I getting these errors regarding that the code blocks #ifndef #define #endif have been used in the code?

Solution

  • As stated by CoryKramer, we don't need the file PolSARpro.c, in fact we should never #include .cpp or .c files and this is what has been done in that file:

    The contents of the file PolSARproLib.c is:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>
    
    #ifdef _WIN32
    #include <dos.h>
    #include <conio.h>
    #endif
    
    /* ROUTINES DECLARATION */
    #include "util.c"
    #include "util_block.c"
    #include "util_convert.c"
    #include "graphics.c"
    #include "matrix.c"
    #include "processing.c"
    #include "statistics.c"
    #include "sub_aperture.c"
    #include "my_utils.c"  
    

    So, in order to get rid of all those 100 LNK2005 errors, simply place these 20 files (Omit PolSARproLib.c) in a folder

    enter image description here

    And do the next steps as stated in the question for creating and building the static library project PolSARproLib.lib file and for creating and building the console application project arii_anned_3components_decomposition.exe file.