I have a project consisting of 6 files; main.cpp, functions.h, tennisplayer.h, tennisplayer.cpp, tennisteam.h & tennisteam.cpp which are roughly defined as follows:
// main.cpp
#include "tennisteam.h"
#include <iostream>
#include <string>
#include <exception>
// some main() code that needs functions.h definitions
// functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "tennisplayer.h"
#include <iostream>
#include <string>
#include <limits>
// some constants & function definitions needed by both main.cpp & tennisteam.cpp
#endif
// tennisplayer.h
#ifndef TENNISPLAYER_H
#define TENNISPLAYER_H
#include <string>
#include <vector>
// tennisplayer class declarations
#endif
// tennisplayer.cpp
#include "tennisplayer.h"
#include <iostream>
#include <fstream>
// tennisplayer class definitions
// tennisteam.h
#ifndef TENNISTEAM_H
#define TENNISTEAM_H
#include "tennisplayer.h"
#include <string>
#include <vector>
//
#endif
// tennisteam.cpp
#include "tennisteam.h"
#include <iostream>
#include <fstream>
// tennisteam class definitions
However, when I include functions.h into both main.cpp & tennisteam.cpp via tennisteam.h I get a linker error along the lines of:
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccv30cX0.o:tennisteam.cpp:(.text+0x0): multiple definition of `function(std::string const&)'; /tmp/ccRThgpp.o:main.cpp:(.text+0x0): first defined here
I'm aware this is a linker error. I've looked around for a fix but all I come across are posts instructing me to use include guards which I have done already. Is there something I'm missing here? Any help would be appreciated.
You have function function(std::string const&) that you not only declared but also defined in your header file. If you need to have it defined there instead of a .cpp file, mark it as inline.
This results in two cpp files (namely main.cpp and tennisteam.cpp) ending up with a definition of that function, because they both include that header file.
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed last year.
//main.cpp
#include <iostream>
#include <array>
#include <iomanip>
#include "Board.h"
#include "Game.h"
//Player.h
#include <array>
#include <string.h>
#include <random>
#include "Property.h"
#include "Game.h"
#ifndef Player_h
#define Player_h
//Property.h
#include "Space.h" //#include nested too deeply error
#ifndef Property_h
#define Property_h
//Space.h
#include <sstream>
#include <string>
#ifndef Space_h
#define Space_h
//FreeParking.h + a couple others inheriting from space; there's no error in any of these either
#include "Space.h"
#ifndef FreeParking_h
#define FreeParking_h
//Board.h
#include "Player.h"
#include <array>
#include <random>
#ifndef Board_h
#define Board_h
//Game.h
#include <array>
#include "Property.h"
#include "CommunityChest.h"
#include "Tax.h"
#include "FreeParking.h"
#include "Jail.h"
#include "GoToJail.h"
#include "Go.h"
#include "Player.h"
#ifndef Game_h
#define Game_h
I don't think I made any changes to the #includes today but just got this error even though the program was running fine 20 minutes ago. I only posted the includes because I'm not sure if the actual code matters for this error or not. If it does I'll try to go over the stuff I wrote today, but most of it was just changing things that already worked previously.
Player.h includes Game.h and Game.h includes Player.h. This is an infinite loop. There might be more, but that's just the first one I saw.
You should remove at least one of those includes to break the infinite loop. If you get errors when you do that, you might be able to fix them using a forward declaration that looks something like this:
class Player;
A forward declaration like that would allow you to compile some code that uses the Player class, even though a complete definition of the Player class is not available at that point in the program.
Two more tips to make things more sane:
Put your include guards at the very top of your file before you include anything.
Use #pragma once as the include guard instead of your more complicated thing.
I have 21 files as shown here in the picture:
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):
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
I have created a folder named Arii and placed the following 22 files in the subfolder \Arii\lib
I have also the file arii_anned_3components_decomposition.c placed in the folder Arii:
Now I create the project arii_anned_3components_decomposition in the folder Arii as follows (here I’ve uploaded the project)
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
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
Properties>Linker>General>Additional library directories
I enter the path to the folder in which .lib files are placed
In
Properties>Linker>Input>Additional Dependencies
I’ve entered name of those needed .lib files:
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:
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)
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?
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
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.
I have the following "includes" file in my project.
#pragma once
//glm
#include <glm\glm.hpp>
#include <glm\ext.hpp>
#include <glm\gtc\matrix_transform.hpp>
//glew
#include "GL\glew.h"
//glfw
#define GLFW_DLL
#include "GLFW\glfw3.h"
//libpng
#include <png.h>
//std
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <memory>
#include <iostream>
#include <fstream>
#include <assert.h>
//boost
#include <boost\filesystem.hpp>
#include <boost\property_tree\json_parser.hpp> /* problem */
//mandala
#include "types.h"
#include "type_traits.h"
#include "hash.h"
#include "macros.h"
When I include <boost\property_tree\json_parser.hpp>, I get many errors indicating that I'm redefining APIENTRY such as this one:
1>c:\program files (x86)\windows kits\8.0\include\shared\minwindef.h(130): warning C4005: 'APIENTRY' : macro redefinition
I'm perplexed as to why this is happening. I've tried to suppress the minwindef.h file from being processed by putting #define _MINWINDEF_ before the include statement but to no avail. Has anyone else encountered this or have any idea how I can properly include this boost library?
NOTE
Since youd did neither update your question to reflect the changes to the includes you made, nor provide the whole warning message, I can only guess:
You still have glfw.h included before the boost lib that includes the WinAPI header. Because when I just google for "APIENTRY redefinition", I get this SO question as first result, including the answer: Put the WinAPI header (or the boost header includign them) before the glfw.h include.
You may want to include also ptree.
#include <boost/property_tree/ptree.hpp>
Why use precompiled headers?
Reading the responses, I suspect what I've been doing with them is kind of stupid:
#pragma once
// Defines used for production versions
#ifndef PRODUCTION
#define eMsg(x) (x) // Show error messages
#define eAsciiMsg(x) (x)
#else
#define eMsg(x) (L"") // Don't show error messages
#define eAsciiMsg(x) ("")
#endif // PRODUCTION
#include "targetver.h"
#include "version.h"
// Enable "unsafe", but much faster string functions
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
// Standard includes
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <direct.h>
#include <cstring>
#ifdef _DEBUG
#include <cstdlib>
#endif
// Standard Template Library
#include <bitset>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
#include <string>
#include <numeric>
// Boost libraries
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/scoped_array.hpp>
//Windows includes
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "FILETIME_Comparisons.h"
#include <shlwapi.h>
#include <Shellapi.h>
#include <psapi.h>
#include <imagehlp.h>
#include <mscat.h>
#include <Softpub.h>
#include <sfc.h>
#pragma comment(lib, "wintrust.lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"Psapi.lib")
#pragma comment(lib,"shlwapi.lib")
#pragma comment(lib,"imagehlp.lib")
#pragma comment(lib,"Advapi32.lib")
#pragma comment(lib,"Shell32.lib")
#pragma comment(lib,"Sfc.lib")
#pragma comment(lib,"Version.lib")
// Crypto ++ libraries
#ifdef _DEBUG
#pragma comment(lib,"cryptlibd.lib")
#else
#pragma comment(lib,"cryptlib.lib")
#endif
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <md5.h>
#include <sha.h>
// String libraries
#include "stringUnicodeConversions.h"
#include "expandEnvStrings.h"
#include "randomString.h"
#include "getShortPathName.h"
// Regular Expression Libraries
#include "fpattern.h"
// File Result Record
#include "unixTimeToFileTime.h"
#include "fileData.h"
// Writer
#include "writeFileData.h"
// Criteria Structure System
#include "priorities.h"
#include "criterion.H"
#include "OPSTRUCT.H"
#include "regexClass.H"
#include "FILTER.h"
// Sub Programs Root Class
#include "subProgramClass.h"
// Global data
#include "globalOptions.h"
// Logger
#include "logger.h"
// Console parser
#include "consoleParser.h"
// Timeout handler
#include "timeoutThread.h"
// Zip library
#include "zip.h"
#include "unzip.h"
#include "zipIt.h"
// Scanner
#include "mainScanner.h"
#include "filesScanner.h"
// Sub Programs
#include "volumeEnumerate.h"
#include "clsidCompressor.h"
#include "times.h"
#include "exec.h"
#include "uZip.h"
// 64 bit support
#include "disable64.h"
In C/C++, the #include mechanism is a textual copy of the file specified into the current file. Headers include other headers (which include yet other headers), so when you do a #include, it could be adding tens of thousands of lines of C++ into each cpp file (or cxx, c, whatever), all of which need to be compiled each time. This can be a severe bottleneck for large projects.
Precompiled headers speed this up by compiling each header once, then including that compiled state into the cpp they are included in.
It compiles a lot quicker. C++ compilation takes years without them. Try comparing some time in a large project!
Re: your current usage, if you have a target with a very large number of files, it may still be faster to use PCH in that way - try switching them off to find out. It depends: if you have a lot of headers of your own, and you change them only infrequently, and you have a very large number of source files that you change much more frequently, then your PCH usage will cut rebuild times.
But normal advice is to only put things in PCH that never change, because there is a certain overhead to producing the PCH itself. If you trigger that off with every rebuild (by constantly tweaking one of your headers), using PCH may make the rebuild slower.
So you don't have to compile them every time you build your project. They're used for system headers that aren't going to change.
It speeds up compilation.
When you're including headers from other projects, you don't expect to change them. If you put these into a precompiled header, then that code will not have to be recompiled when you make changes to your source code. This reduces repetitive compilation of unchanged code, speeding up compile time.