I recently worked on a project that uses a encapsulated dynamic library (lib + dll). The header file defines the export type as: # ifdef NetLink
# Define NETDIR _ declspec (dllexport)
# Else
# Define NETDIR _ declspec (dllimport)
# Endif
NETDIR void LinkDir (); this dynamic library has no problems in compilation and usage. However, can someone convert a dynamic library into a static library?
Therefore, first try to change the compilation option in Visual Studio to the "static library lib" mode without any modification in the Code. As a result, the compilation fails in the referenced project and an error LNK2019 is reported: an error occurred while parsing external symbols.
Is it because the dynamic library method was selected before the project was created, so it is not possible? So I tried to recreate the project, select the static library method, and port the code as a whole. The error is still returned.
I tried to search for the header file in the following format: # ifdef NetLink
# Define NETDIR extern "C" _ declspec (dllexport)
# Else
# Define NETDIR extern "C" _ declspec (dllimport)
# Endif
NETDIR void LinkDir (); After compiling and referencing the project, the error information is changed. Many of the previous unsigned information is changed to error LNK2019: the external symbol cannot be parsed error "_ imp_LinkDir () ". However, compilation fails.
Change the final header file
# Define NETDIR extern "C"
NETDIR void LinkDir ();
Compiled.
Summary:
To define the export function in the static library mode, you only need to add extern "C" in front!
Note one article.