wjwwood / serial

Cross-platform, Serial Port library written in C++
http://wjwwood.github.com/serial/
MIT License
2.11k stars 1.03k forks source link

Unresolved symbol error visual studio 2017 #170

Open Gunlek opened 6 years ago

Gunlek commented 6 years ago

Everytime i try to compile with visual studio 2017, i'm getting in trouble with an unresolved symbol error (LNK2019)... How can I fix that ?

wjwwood commented 6 years ago

Please post the full build output, without it there is no context for me to deduce what the issue might be.

Gunlek commented 6 years ago

Of course, here's the build output I get:

1>------ Début de la génération : Projet : SerialTest, Configuration : Debug x64 ------
1>Main.obj : error LNK2019: unresolved external symbol "class std::vector<struct serial::PortInfo,class std::allocator<struct serial::PortInfo> > __cdecl serial::list_ports(void)" (?list_ports@serial@@YA?AV?$vector@UPortInfo@serial@@V?$allocator@UPortInfo@serial@@@std@@@std@@XZ) referenced in main function
1>PATH_TO_PROJECT\x64\Debug\SerialTest.exe : fatal error LNK1120: 1 externes non résolus
1>Génération du projet "SerialTest.vcxproj" terminée -- ÉCHEC.
========== Génération : 0 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========

I'm french and so, output is french, I translated errors in english for you

wjwwood commented 6 years ago

Thanks, typically, this kind of error will occur when either you have not linked against the serial library (not sure how to express that in Visual Studio) or in this specific linker error perhaps you're building serial.cc yourself, but have excluded the "list_ports" part of the library.

klukander commented 3 years ago

Hi, I ran into this with VS2019 and banged my head against the wall for quite some time with the typical visual studio linking things all in order. I then noticed that this happens with two functions in serial requiring registry access:

Error LNK2019 unresolved external symbol __imp_RegCloseKey referenced in function "class std::vector<struct serial::PortInfo,class std::allocator > __cdecl serial::list_ports(void)" (?list_ports@serial@@YA?AV?$vector@UPortInfo@serial@@V?$allocator@UPortInfo@serial@@@std@@@std@@XZ)

Error LNK2019 unresolved external symbol __imp_RegQueryValueExW referenced in function "class std::vector<struct serial::PortInfo,class std::allocator > __cdecl serial::list_ports(void)" (?list_ports@serial@@YA?AV?$vector@UPortInfo@serial@@V?$allocator@UPortInfo@serial@@@std@@@std@@XZ)

And then from a random stackoverflow response I already lost, found out that these need to be linked against Advapi32.lib

Adding that to my project's linked libraries seems to solve the issue.

I have no previous experience on this, and I don't quite understand why compiling the serial library itself, and the test_serial project included don't have an issue with this (the test runs fine).

Charlie-Gallie commented 3 months ago

Apologies to add to such an old thread, but if anybody else encounters this issue even with the files included in your project and linked to Advapi32.lib, try changing the .cc file extensions to .cpp.

My version of Visual Studio doesn't compile .cc files by default. Changing them to .cpp fixed it.

An alternative issue which can sometimes cause the same symptoms is if you have two .cpp/.cc files with the same name, e.g., serial.cc (the one this library uses) and Serial.cpp (your serial interface for your own project). Case sensitivity isn't always a thing, so try renaming your implementation to something different.