I'm compiling some code that relies on include guards to prevent multiple definitions of objects and functions, but Visual Studio 2008 is giving me link errors that there are multiple definitions. I don't understand why because I've used code very similar to this before and it hasn't caused problems. I must be doing something dumb but I have no idea what it is. I also tried to take out the include guards and use #pragma once, but I get the same link errors. What should I check for?
If they are linker errors, the most likely cause is probably non-inline functions defined in the header.
If you have a non-inline function in a header that is included in more than one source file, it will be defined in each of those source files ("translation units"), thus the function will be defined more than once, hence the multiple definitions error.
If you're getting linker errors... are you sure you're not 1) actually defining a function twice in code or 2) trying to do something silly like #include a source file (as opposed to a header file)?
This can also be caused by using different versions of the cstd lib from other library's linked in. Check under the c++/Code generation section and make sure all your projects are using the same settings.
Related
This question is about something that after more than a year with C++ I can't solve or find any solution about it.
I got used to using separate files for headers and code in C, but I have a problem with it on C++: whenever I edit a header file and try to compile the code that uses it again, the compiler doesn't notice the change on the header.
What I do to solve this is "compiling" the header (.hpp) alone. Sometimes I just add it to the list of source files for g++ along with the rest of the code, but what happens then is that I have to execute the command twice (the first time it gives me errors, but not the second time). It also warns me that I'm using the "pragma once" option in a main file.
I know this is very wrong, so I've searched for a correct way to do this, without success. I have noticed that g++ generates ".gch" files but I don't really know what's their purpose, although they may be related.
I suspect that the problem is caused because of the code in the ".hpp". I know (I think) that the good way to do it is to define prototypes only inside the header and writing the body of the methods in a separate file, but sometimes (specially when using templates) this generates even more problems.
The .gch is a precompiled header and it is created if you explicitly compile a header file.
The compiler will then use that file instead of the actual header (the compiler does not care about modification timestamps).
Do rm *.gch and leave all headers out of the compilation command forever.
(And don't put template implementations in .cpp files.)
I try to include headers from a library in different files of my project and I get multiple definition errors on some functions of the library.
After reading the answer to this question I think the problem is that the functions are implemented directly in the header files of the library.
In particular I want to include the files codecfactory.h and deltautil.h from FastPFor. I don't know if it is relevant for my problem but I include it into my cmake project with this code in my CMakeLists.txt:
include_directories(../../FastPFor/headers)
add_library(FastPFor STATIC ../../FastPFor/src/bitpacking.cpp
../../FastPFor/src/bitpacking.cpp
../../FastPFor/src/bitpackingaligned.cpp
../../FastPFor/src/bitpackingunaligned.cpp
../../FastPFor/src/horizontalbitpacking.cpp
../../FastPFor/src/simdunalignedbitpacking.cpp
../../FastPFor/src/simdbitpacking.cpp
${HEADERS}
)
Everything works fine if I just include the files once. But as soon as I use them in two .cpp files I get these kinds of errors:
CMakeFiles/dbgen.bin.dir/queries/Query5.cpp.o: In function `vsencoding::BitsWriter::BitsWriter(unsigned int*)':
Query5.cpp:(.text+0x8420): multiple definition of `vsencoding::BitsWriter::BitsWriter(unsigned int*)'
CMakeFiles/dbgen.bin.dir/queries/Query13Naive.cpp.o:Query13Naive.cpp:(.text+0x7a50): first defined here
Is there any way I can fix this without having to change the FastPFor code but only my own?
The question you linked to says it all - there is no way to solve this without modifying the headers (or just include them in only one source file).
For instance this line defines a non-inline constructor in a header. Including it in more than one translation unit would result in a violation of the ODR rule.
One way to workaround this would be to change your project to header only style, i.e. moving your implementation to header files. In this way you can keep (more ore less) the structure of your project.
However, this is definitely not a nice solution... The whole project needs to be compiled after every small change to one of the headers...
I'm in VS2013, C++ console applications. I'm having a problem integrating boost into a large framework. If I try integrating them in a blank console application, they work fine. Once I include the "root" .h file of the framework (that includes "many" other .h files in the bargain), it breaks. These .h files are "polluting" the boost ones (and anything included after, with mixed results, and no, I can't just include boost ones first, that's not always an option unfortunately). I've found at least one root-level #define that interfered and caused a compile error, but I can't find some of the other conflicts that are causing run-time problems.
Specifically, my problem is this: how do I tell what symbols have been defined by .h files? And hopefully, which ones are then conflicting later? I tried googling, but couldn't find a tool for doing this.
Or is there some other method which can "isolate" them (my problem .h files), and yet still have them link correctly to the functions they're calling in other .dlls?
You can use g++ -E as a static code checking tool (without changing your toolset). It is able to tell you when something is redefined but not when a #define is used as another name (it would have no way to tell whether it was a real substitution or not).
If that's not the source of your problem then you may need to take a more holistic approach: Start changing your project's #define use to other constructs such as const and short functions. This will then allow the compiler to either resolve differences by overloading or complain that there are conflicts.
Including same header file again might have caused the problem,you can create a symbol for each header file so that if that header file is already included in some other header file it shouldn't be included.
#ifndef
#define __header_file_name_H
.....some code
#endif
When I include some function from a header file in a C++ program, does the entire header file code get copied to the final executable or only the machine code for the specific function is generated. For example, if I call std::sort from the <algorithm> header in C++, is the machine code generated only for the sort() function or for the entire <algorithm> header file.
I think that a similar question exists somewhere on Stack Overflow, but I have tried my best to find it (I glanced over it once, but lost the link). If you can point me to that, it would be wonderful.
You're mixing two distinct issues here:
Header files, handled by the preprocessor
Selective linking of code by the C++ linker
Header files
These are simply copied verbatim by the preprocessor into the place that includes them. All the code of algorithm is copied into the .cpp file when you #include <algorithm>.
Selective linking
Most modern linkers won't link in functions that aren't getting called in your application. I.e. write a function foo and never call it - its code won't get into the executable. So if you #include <algorithm> and only use sort here's what happens:
The preprocessor shoves the whole algorithm file into your source file
You call only sort
The linked analyzes this and only adds the source of sort (and functions it calls, if any) to the executable. The other algorithms' code isn't getting added
That said, C++ templates complicate the matter a bit further. It's a complex issue to explain here, but in a nutshell - templates get expanded by the compiler for all the types that you're actually using. So if have a vector of int and a vector of string, the compiler will generate two copies of the whole code for the vector class in your code. Since you are using it (otherwise the compiler wouldn't generate it), the linker also places it into the executable.
In fact, the entire file is copied into .cpp file, and it depends on compiler/linker, if it picks up only 'needed' functions, or all of them.
In general, simplified summary:
debug configuration means compiling in all of non-template functions,
release configuration strips all unneeded functions.
Plus it depends on attributes -> function declared for export will be never stripped.
On the other side, template function variants are 'generated' when used, so only the ones you explicitly use are compiled in.
EDIT: header file code isn't generated, but in most cases hand-written.
If you #include a header file in your source code, it acts as if the text in that header was written in place of the #include preprocessor directive.
Generally headers contain declarations, i.e. information about what's inside a library. This way the compiler allows you to call things for which the code exists outside the current compilation unit (e.g. the .cpp file you are including the header from). When the program is linked into an executable that you can run, the linker decides what to include, usually based on what your program actually uses. Libraries may also be linked dynamically, meaning that the executable file does not actually include the library code but the library is linked at runtime.
It depends on the compiler. Most compilers today do flow analysis to prune out uncalled functions. http://en.wikipedia.org/wiki/Data-flow_analysis
while programming in c++,
Most of the times i get 'some symbol' has already been defined
see previous definition of 'some symbol'
I think this happens because improper order of headers file included.
How can i find out all the definitions of 'some symbol'
Thanks in advance,
Uday
EDIT:
I am using visual studio
I remember some command some thing like dumpbin /exports (I dont remember exactly) to get all the definition
That usually happens due to a couple of common problems:
you are redefining the same symbol
you are including a header file many times and it does not have include guards
you are incorrectly forward declaring with an incompatible type and then including
I would start trying to fix the second. Make sure that all headers have include guards so that if you include them from different places it won't try to redefine the symbols:
#ifndef HEADER_FILE_NAME_GUARD // or something alike
#define HEADER_FILE_NAME_GUARD
// rest of file
#endif
If you need to order your includes then you will run into troubles later, make sure that you can and do include all your dependencies at all places. (Or forward declare if you can)
Full-text search is the most cross-platform cross-environment and efficient technique for that.
Translating C++ into an executbale has two steps. In step one, the compiler works linearly through the inputs (typically .cpp files and the headers they include). In step 2, the linker combines the results from step 1.
From your description of "symbol defined before", I conclude the problem must occur within a .cpp file, as those are processed linearly. dumpbin /exports works on the output of step 1, which you probably won't have. Header inclusion could be the culprit, as that's an early phase of compilation. You want the preprocessed input. IIRC, you can get that with the /EP switch.
If you're using Visual Studio with Visual Assist installed, alt-G will pop up a list of all places where something is defined.
If you're using Visual Studio with Visual Assist installed, you'd better press Alt+X then D to see all the references to the symbol
If you need to order includes, it is an indication of 'Code Smell' in the first place. Most probably, your code is not designed well enough. All your common code should be part of a separate package/dll/lib(whatever) and should be included/linked from there.
If you are in C++/VC++, this normally happens when you include cpp & h files directly from other projects instead of linking to a utility library of some sort.
As somebody already pointed out, if you have to live with it, text search is the best option but try re-designing the code in a better way (if you can).
Generally I just grep the header files.