Closed wbpcode closed 3 years ago
Let me do some research first
https://github.com/wbpcode/hessian2-codec/tree/52e51c4602d64db95b5b7484ca8229254fee6a2f
Checkout this repo and commit and run:
bazel build //example/mutiple_src:mutiple_src
It will be multiple definition error.
And checkout to 7ab86600f9a0eceea137051ce2da21282400716f, run above command again, it will be ok. (7ab86600f9a0eceea137051ce2da21282400716f moved function definition to .cc)
If a function template specialization specifies all template parameters, then it is a normal function, not a template. The declaration and implementation should be separated in this case to avoid redefinition errors.
@zyfjeff
@wbpcode You're right. That seems to be the reason. https://stackoverflow.com/questions/4445654/multiple-definition-of-template-specialization-when-using-different-objects
I I have completed a version in which the definitions of related functions have been moved to the cc file. Of course, directly declaring related functions as inline can also solve this problem. If you prefer the former, I can submit a PR directly. If you prefer the latter, May be I can take the time to prepare another PR tomorrow to solve this problem.
@zyfjeff
@wbpcode The former is better, and separating implementation and declaration is a best practice.
If we need to decode/encode string/int32_t etc. in different
.cc
files, we need to include the header file in basic_codec either directly or indirectly in the.cc
files. In this case, some duplicated symbol errors occur, because some methods are defined in different compilation units.Maybe we need to declare the template specialization methods in basic_codec as inline or extern to solve this problem.