Skip to content

Fixes errors when linking caffe2 statically#10765

Closed
peterjc123 wants to merge 3 commits intopytorch:masterfrom
peterjc123:link_fix
Closed

Fixes errors when linking caffe2 statically#10765
peterjc123 wants to merge 3 commits intopytorch:masterfrom
peterjc123:link_fix

Conversation

@peterjc123
Copy link
Collaborator

@peterjc123 peterjc123 commented Aug 22, 2018

Fixes #10746 and #10902.
The linking error is caused by the wrong usage of dllexport/dllimport. We don't need them if the implementation of the function is available from the external side.

Copy link
Contributor

@orionr orionr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If tests pass, this looks good. Thanks!

@orionr
Copy link
Contributor

orionr commented Aug 22, 2018

@pytorchbot retest this please

1 similar comment
@orionr
Copy link
Contributor

orionr commented Aug 22, 2018

@pytorchbot retest this please

@peterjc123
Copy link
Collaborator Author

@pytorchbot retest this please

1 similar comment
@peterjc123
Copy link
Collaborator Author

@pytorchbot retest this please

@peterjc123 peterjc123 force-pushed the link_fix branch 2 times, most recently from e6a7b47 to 236d0b4 Compare August 25, 2018 02:49
@peterjc123
Copy link
Collaborator Author

I think the failure of CI is unrelated to this PR.

@peterjc123
Copy link
Collaborator Author

@pytorchbot retest this please

@xkszltl
Copy link
Contributor

xkszltl commented Aug 28, 2018

@peterjc123

I was trying to fix this in a slightly different way and also notice some other suspicious places.

Should we also fix the linkage in its base class IdWrapper?

@xkszltl
Copy link
Contributor

xkszltl commented Aug 28, 2018

Added #10941

@peterjc123
Copy link
Collaborator Author

peterjc123 commented Aug 28, 2018

@xkszltl Yes, that's missing. Thanks for pointing out. For the convenience of the merging of these related PRs, I've included all the required changes in this PR. So, would you please close your PR(s)?

@peterjc123 peterjc123 changed the title Fixes linking errors in dispatch_test of caffe2 Fixes linking errors when linking caffe2 statically Aug 28, 2018
@peterjc123 peterjc123 changed the title Fixes linking errors when linking caffe2 statically Fixes errors when linking caffe2 statically Aug 28, 2018
@peterjc123
Copy link
Collaborator Author

@soumith @ezyang Can we please merge this PR?

@orionr
Copy link
Contributor

orionr commented Aug 28, 2018

Hidden visibility (for *nix) is landing in #10752, so let's wait until that and then rebase and test. Hopefully after the above we should get better signal on Unix builds for Windows dllimport/export issues. Granted, not perfect, but better.

@orionr
Copy link
Contributor

orionr commented Aug 28, 2018

@pytorchbot retest this please

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orionr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@bddppq
Copy link
Contributor

bddppq commented Aug 29, 2018

@pytorchbot retest this please

@peterjc123
Copy link
Collaborator Author

@orionr It seems the problem comes to GCC this time. The build failure is due to this change. However, the template is already wrapped with the annotation here.

@orionr
Copy link
Contributor

orionr commented Aug 29, 2018

@peterjc123, correct - looks like we need to have class AT_CORE_API TypeIdentifier, since the tests reference that class, not just the methods. I'll also see if I can leave any comments on the changes here.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@peterjc123
Copy link
Collaborator Author

peterjc123 commented Sep 2, 2018

@orion It seems that you are right. As this post says, it seems that GCC wants the entire class exported while MSVC doesn't. So there may be two possible solutions for this issue:

  1. Use a macro for GCC to export entire class and keep minimal with MSVC. (Not so good but easy to do)
  2. Move the implementation into the source file to make the class-level export work with MSVC. (Seems better but may be difficult to do)

@orionr
Copy link
Contributor

orionr commented Sep 4, 2018

Thanks for the feedback. @peterjc123, depending on how many cases we have of this, I would recommend exploring option 2 for a day or two and then fallback to option 1. @mingzhe09088 is also seeing challenges with enabling all of Caffe2 with shared DLL builds on Windows, so it's possible that we'll hit similar issues there. So maybe option 1 is going to be required regardless.

@orionr
Copy link
Contributor

orionr commented Sep 4, 2018

For reference, @mingzhe09088 is doing this work at #11160

@xkszltl
Copy link
Contributor

xkszltl commented Sep 5, 2018

@peterjc123

For that stackoverflow post, I wonder if it's because implicitly defined functions, e.g. default constructor, are not exported unless we put the decorator at class level.

If that's the case, something like this might help:

class A
{
    CAFFE2_EXPORT A() = default;
};

@peterjc123
Copy link
Collaborator Author

@xkszltl Thanks! I'll try this.

@peterjc123
Copy link
Collaborator Author

peterjc123 commented Sep 5, 2018

@xkszltl Looks like it wasn't the situation that you stated. Actually, the problem is caused by the unexported symbol CAFFE_KNOWN_TYPE for GCC. But it is impossible to set the attribute visibility:default on it. See details here.

Test locally and figure out all the required changes

Target comments

minor changes

.
*/
template <typename T>
AT_CORE_API static TypeIdentifier Id();
static TypeIdentifier Id();

This comment was marked as off-topic.

This comment was marked as off-topic.

@orionr
Copy link
Contributor

orionr commented Sep 7, 2018

@peterjc123 do you still have issues with building static libs on Windows with the latest master? Just checking. I feel like with the changes @Yangqing submitted we should be in good shape without these changes, but please let us know.

@peterjc123
Copy link
Collaborator Author

@orionr Let me try locally first.

@peterjc123
Copy link
Collaborator Author

peterjc123 commented Sep 8, 2018

@orionr It seems this issue is not resolved by the several Windows PRs. The error log is here:

  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: __cdecl caffe2::TypeMeta:
:TypeMeta(class caffe2::TypeIdentifier,unsigned __int64,void (__cdecl*)(void *,unsigned __int64),void (__cdecl*)(void c
onst *,void *,unsigned __int64),void (__cdecl*)(void *,unsigned __int64))" (__imp_??0TypeMeta@caffe2@@AEAA@VTypeIdentif
ier@1@_KP6AXPEAX1@ZP6AXPEBX21@Z3@Z) referenced in function "public: __cdecl caffe2::TensorImpl::TensorImpl<float>(class
 std::vector<__int64,class std::allocator<__int64> > const &,class std::vector<float,class std::allocator<float> > cons
t &,class caffe2::BaseContext *)" (??$?0M@TensorImpl@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vecto
r@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [D:\pytorch\caffe2-static-minimal\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: unsigned __int64 const & _
_cdecl caffe2::TypeMeta::itemsize(void)const " (__imp_?itemsize@TypeMeta@caffe2@@QEBAAEB_KXZ) referenced in function "p
ublic: __cdecl caffe2::TensorImpl::TensorImpl<float>(class std::vector<__int64,class std::allocator<__int64> > const &,
class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@TensorImpl@caffe2@@
QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [D:\pytorch\c
affe2-static-minimal\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caff
e2::TypeMeta::ctor(void)const )(void *,unsigned __int64)" (__imp_?ctor@TypeMeta@caffe2@@QEBAP6AXPEAX_K@ZXZ) referenced
in function "public: void * __cdecl caffe2::TensorImpl::raw_mutable_data(class caffe2::TypeMeta const &)" (?raw_mutable
_data@TensorImpl@caffe2@@QEAAPEAXAEBVTypeMeta@2@@Z) [D:\pytorch\caffe2-static-minimal\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caff
e2::TypeMeta::copy(void)const )(void const *,void *,unsigned __int64)" (__imp_?copy@TypeMeta@caffe2@@QEBAP6AXPEBXPEAX_K
@ZXZ) referenced in function "public: __cdecl caffe2::TensorImpl::TensorImpl<float>(class std::vector<__int64,class std
::allocator<__int64> > const &,class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext
*)" (??$?0M@TensorImpl@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAV
BaseContext@1@@Z) [D:\pytorch\caffe2-static-minimal\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caff
e2::TypeMeta::dtor(void)const )(void *,unsigned __int64)" (__imp_?dtor@TypeMeta@caffe2@@QEBAP6AXPEAX_K@ZXZ) referenced
in function "public: void * __cdecl caffe2::TensorImpl::raw_mutable_data(class caffe2::TypeMeta const &)" (?raw_mutable
_data@TensorImpl@caffe2@@QEAAPEAXAEBVTypeMeta@2@@Z) [D:\pytorch\caffe2-static-minimal\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class caffe2::TypeI
dentifier __cdecl caffe2::TypeMeta::Id<float>(void)" (__imp_??$Id@M@TypeMeta@caffe2@@SA?AVTypeIdentifier@1@XZ) referenc
ed in function "public: __cdecl caffe2::TensorImpl::TensorImpl<float>(class std::vector<__int64,class std::allocator<__
int64> > const &,class std::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@Te
nsorImpl@caffe2@@QEAA@AEBV?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@
@Z) [D:\pytorch\caffe2-static-minimal\build\test-caffe2.vcxproj]
  test-caffe2.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) protected: __cdecl c10::intrusive_
ptr_target::intrusive_ptr_target(void)" (__imp_??0intrusive_ptr_target@c10@@IEAA@XZ) referenced in function "public: __
cdecl caffe2::TensorImpl::TensorImpl<float>(class std::vector<__int64,class std::allocator<__int64> > const &,class std
::vector<float,class std::allocator<float> > const &,class caffe2::BaseContext *)" (??$?0M@TensorImpl@caffe2@@QEAA@AEBV
?$vector@_JV?$allocator@_J@std@@@std@@AEBV?$vector@MV?$allocator@M@std@@@3@PEAVBaseContext@1@@Z) [D:\pytorch\caffe2-sta
tic-minimal\build\test-caffe2.vcxproj]

@orionr
Copy link
Contributor

orionr commented Sep 10, 2018

Looks like tests are failing to link. @mingzhe09088 is working on BUILD_PYTHON enabling at #11385 and then we're going to look at BUILD_TEST. Both are by default disabled on Windows right now, which they shouldn't be.

@orionr
Copy link
Contributor

orionr commented Sep 10, 2018

cc @Yangqing

@peterjc123
Copy link
Collaborator Author

Closed because it's out of date.

@peterjc123 peterjc123 closed this Dec 26, 2018
@peterjc123 peterjc123 deleted the link_fix branch April 5, 2019 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Caffe2] Failed to build dispatch_test. Error LNK2001: unresolved external symbol

6 participants