C language dynamic library to static library

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.