Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS2017 link error when using antlrcpp::Any #2211

Closed
eternalphane opened this issue Jan 25, 2018 · 10 comments
Closed

VS2017 link error when using antlrcpp::Any #2211

eternalphane opened this issue Jan 25, 2018 · 10 comments

Comments

@eternalphane
Copy link
Contributor

Hello. I'm trying to use antlrcpp::Any and got the following errors when building with VS2017:

error LNK2019: unresolved external symbol "public: virtual __thiscall antlrcpp::Any::Base::~Base(void)" (??1Base@Any@antlrcpp@@UAE@XZ) referenced in function "public: virtual __thiscall antlrcpp::Any::Derived<int>::~Derived<int>(void)" (??1?$Derived@H@Any@antlrcpp@@UAE@XZ)
fatal error LNK1120: 1 unresolved externals

Test.cpp:

#include <antlr4-runtime.h>
#include <iostream>

int main()
{
	int i = 0;
	antlrcpp::Any test(i);
	std::cout << test.as<int>() << std::endl;
	return 0;
}

However, if I move the definition of antlrcpp::Any::Base::~Base() inline, it works as expected. I don't know whether this is a bug of VS2017 or my usage of antlrcpp::Any is wrong in the above code. Could you please address this issue?

@Chengifei
Copy link

Chengifei commented Feb 2, 2018

I have the same problem, a wild guess might be the symbol is being not exported. (Plus, I don't see the point to put a do-nothing destructor in to a separate .cpp file.)

@kfwalther
Copy link

I also had the same problem when extending the TParserBaseVisitor class with my own visitor class. The antlrcpp::Any::Base::~Base() in the .cpp file isn't seen by the linker for some reason. As @eternalphane suggested, copying the destructor definition inline into my custom Visitor class header file fixed the issue. I've only tested on VS2017, so not sure if the other VS versions are seeing the same problem. Maybe @mike-lischke can provide insight on the issue.

@mike-lischke
Copy link
Member

Unfortunately, I missed the fact that the Any class is not exported, when I approved the patch which changed the d-tor. Previously it was a header only class. In fact I think now that move of the d-tor to a cpp file was unnecessary, mea culpa. So, the best solution is to move the d-tor back to the header.

@eternalphane
Copy link
Contributor Author

@mike-lischke I've made a pr for this issue. Please check #2213. Thanks.

@parrt parrt closed this as completed in 6b8e982 Jun 16, 2018
parrt added a commit that referenced this issue Jun 16, 2018
@siliconvoodoo
Copy link

it still doesn't work but now with the move constructor for some reason. and a bunch of other functions.

error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl antlrcpp::Any::Any(struct antlrcpp::Any &&)" (__imp_??0Any@antlrcpp@@QEAA@$$QEAU01@@Z) error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::visit(class antlr4::tree::ParseTree *)" (__imp_?visit@AbstractParseTreeVisitor@tree@antlr4@@UEAA?AUAny@antlrcpp@@PEAVParseTree@23@@Z) error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl antlr4::tree::AbstractParseTreeVisitor::AbstractParseTreeVisitor(void)" (__imp_??0AbstractParseTreeVisitor@tree@antlr4@@QEAA@XZ) error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl antlr4::tree::AbstractParseTreeVisitor::~AbstractParseTreeVisitor(void)" (__imp_??1AbstractParseTreeVisitor@tree@antlr4@@UEAA@XZ) error LNK2001: unresolved external symbol "protected: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::aggregateResult(struct antlrcpp::Any,struct antlrcpp::Any const &)" (?aggregateResult@AbstractParseTreeVisitor@tree@antlr4@@MEAA?AUAny@antlrcpp@@U45@AEBU45@@Z) error LNK2001: unresolved external symbol "protected: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::defaultResult(void)" (?defaultResult@AbstractParseTreeVisitor@tree@antlr4@@MEAA?AUAny@antlrcpp@@XZ) error LNK2001: unresolved external symbol "protected: virtual bool __cdecl antlr4::tree::AbstractParseTreeVisitor::shouldVisitNextChild(class antlr4::tree::ParseTree *,struct antlrcpp::Any const &)" (?shouldVisitNextChild@AbstractParseTreeVisitor@tree@antlr4@@MEAA_NPEAVParseTree@23@AEBUAny@antlrcpp@@@Z) error LNK2001: unresolved external symbol "public: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::visit(class antlr4::tree::ParseTree *)" (?visit@AbstractParseTreeVisitor@tree@antlr4@@UEAA?AUAny@antlrcpp@@PEAVParseTree@23@@Z) error LNK2001: unresolved external symbol "public: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::visitChildren(class antlr4::tree::ParseTree *)" (?visitChildren@AbstractParseTreeVisitor@tree@antlr4@@UEAA?AUAny@antlrcpp@@PEAVParseTree@23@@Z) error LNK2001: unresolved external symbol "public: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::visitErrorNode(class antlr4::tree::ErrorNode *)" (?visitErrorNode@AbstractParseTreeVisitor@tree@antlr4@@UEAA?AUAny@antlrcpp@@PEAVErrorNode@23@@Z) error LNK2001: unresolved external symbol "public: virtual struct antlrcpp::Any __cdecl antlr4::tree::AbstractParseTreeVisitor::visitTerminal(class antlr4::tree::TerminalNode *)" (?visitTerminal@AbstractParseTreeVisitor@tree@antlr4@@UEAA?AUAny@antlrcpp@@PEAVTerminalNode@23@@Z) fatal error LNK1120: 11 unresolved externals

@siliconvoodoo
Copy link

tried the ParseTreeWalker and same problem with 3 functions. (constr/destr and walk)

@siliconvoodoo
Copy link

siliconvoodoo commented Oct 11, 2018

dumpbin says the symbols appear to be here though. it's the same the linker complains about not finding.
image

not sure. could be because it's expecting an _imp. Visual studio outputs is:
Error LNK2001 unresolved external symbol "__declspec(dllimport) public: virtual void __cdecl antlr4::tree::ParseTreeWalker::walk(class antlr4::tree::ParseTreeListener *,class antlr4::tree::ParseTree *)const " (__imp_?walk@ParseTreeWalker@tree@antlr4@@UEBAXPEAVParseTreeListener@23@PEAVParseTree@23@@Z)

EDIT:
yes removing ANTLR4CPP_PUBLIC from the class ParseTreeWalker fixes the problem.
(because I'm using the _static version of the lib.)

@mike-lischke
Copy link
Member

mike-lischke commented Oct 12, 2018

Yet another case of not compiled object code? Anyway, you should open a new issue instead of re-using a closed one.

Maybe we should go C++17 and use their variant (or any) class? This will limit the user base to those having upgraded or are willing to upgrade to C++17, but being a bit progressive is not a bad thing anyway (and the previous version would still be available for those that don't want).

@siliconvoodoo
Copy link

siliconvoodoo commented Oct 12, 2018

I need to test the dynamic link first. but the fix is probably less drastic, like detecting static link and requiring to define ANTLR4CPP_STATIC in the client project.
At least if google can match the error message and return this advice, it's good too. So it's important that we talk about it.

@mike-lischke
Copy link
Member

@siliconvoodoo I've created a new PR (#2383) which adds VS 2017 project + solution files. They build fine for me for all targets (DLL, static lib, debug + release). See if that works for you too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants