alibaba / hessian2-codec

hessian2-codec it is a complete C++ implementation of hessian2 spec
Apache License 2.0
26 stars 9 forks source link

duplicated symbol error #5

Closed wbpcode closed 3 years ago

wbpcode commented 3 years ago

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.

zyfjeff commented 3 years ago

Let me do some research first

wbpcode commented 3 years ago

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)

wbpcode commented 3 years ago

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

zyfjeff commented 3 years ago

@wbpcode You're right. That seems to be the reason. https://stackoverflow.com/questions/4445654/multiple-definition-of-template-specialization-when-using-different-objects

wbpcode commented 3 years ago

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

zyfjeff commented 3 years ago

@wbpcode The former is better, and separating implementation and declaration is a best practice.