尽管在我的.h文件中使用了`#ifndef #define#endif`代码块,为什么会出现这么多`LNK2005`错误?

时间:2017-09-19 16:00:31

标签: c++ visual-studio visual-studio-2013 linker lnk2005

我有21个文件,如图所示:

enter image description here

以下35个函数在

util.h的第364-411行中声明
#ifndef FlagUtil

#define FlagUtil

#endif  

代码块:

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  

util_block.h的第99-135行声明了以下30个函数
#ifndef FlagUtilBlock

#define FlagUtilBlock

#endif  

代码块:

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  

以下35个函数在

中的util_convert.h的第106-149行中声明
#ifndef FlagUtilConvert

#define FlagUtilConvert

#endif  

代码块:

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  

文件PolSARproLib.h的内容是:

#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"  

文件PolSARproLib.c的内容是:

#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"  

我已将这21个文件放在名为Static Project的文件夹中,我创建了PolSARproLib.lib文件,如下所示,我的机器是Win 10x64文件:(here我' ve上传了这个项目):

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

文件graphics.objmatrix.objmy_utils.objPolSARproLib.libPolSARproLib.objprocessing.objstatistics.obj,{{1 }},sub_aperture.objutil.objutil_block.obj在项目的util_convert.obj文件夹中创建,没有任何错误,但有些警告如下:

Debug

enter image description here

enter image description here

我创建了一个名为1>graphics.obj : warning LNK4006: _write_header_bmp_8bit already defined in PolSARproLib.obj; second definition ignored 的文件夹,并将以下22个文件放在子文件夹Arii

enter image description here

我还将文件\Arii\lib放在文件夹arii_anned_3components_decomposition.c中:

enter image description here

现在我在文件夹Arii中创建项目arii_anned_3components_decomposition,如下所示(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

enter image description here

在以下2张图片中,您可以看到项目的配置。我应该说我正在使用Win 10x64机器

enter image description here

enter image description here

外部库按如下方式添加到项目中:

Arii

我输入了放置标题文件Properties>C/C++>General>Additional Include directories 的文件夹的路径

enter image description here

.h

我输入了放置Properties>Linker>General>Additional library directories 个文件的文件夹的路径

enter image description here

.lib

我输入了所需Properties>Linker>Input>Additional Dependencies 个文件的名称:

enter image description here

这是文件.lib的开头(主函数之前):

arii_anned_3components_decomposition.c

现在我构建解决方案:

enter image description here

不幸的是,我得到了104个错误。其中100个是关于#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" util.hutil_convert.h中函数的多重定义(这些函数列在问题的开头)

错误1-100是相似的:

util_block.h

enter image description here

Here我上传了1>PolSARproLib.lib(PolSARproLib.obj) : error LNK2005: _C3_to_T3 already defined in PolSARproLib.lib(util_convert.obj)

  • 我真的不明白为什么我会收到这些错误,因为代码块error and warning file.txt已用于 代码吗

1 个答案:

答案 0 :(得分:0)

正如CoryKramer所述,我们不需要文件PolSARpro.c,实际上我们不应该#include .cpp.c个文件,这是在该文件中做了什么:

文件PolSARproLib.c的内容是:

#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"  

因此,为了摆脱所有这100个LNK2005错误,只需将这20个文件(忽略PolSARproLib.c)放在一个文件夹中

enter image description here

按照问题中的说明执行以下步骤,以创建和构建静态库项目PolSARproLib.lib文件,以及创建和构建控制台应用程序项目arii_anned_3components_decomposition.exe文件。