Can not import static library with c++

Phan Hoài Sơn 40 Reputation points
2023-06-27T08:01:59.2933333+00:00

Hey guys,

I'm new c++ developer.
I want to build custom static library by my self.
I built success static lib c++ visual code
Now I want to import my static lib to other c++ code in visual studio code. But it not work
Here is my code:

#visual code: build success : mylib.lib
User's image

#Visual studio code
User's image

Developer technologies | C++
Developer technologies | Visual Studio | Other
{count} votes

Accepted answer
  1. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-06-27T09:31:03.8033333+00:00

    Hi,

    The format .lib are compiled with visual studio: you can't use it simply in g++.

    Below is my test sample and I build the dll in x86:
    MSVC DLL:

    #pragma once
    #include <iostream>
    
    #ifdef MSVC_EXPORTS
    #define API __declspec(dllexport)
    #else
    #define API __declspec(dllimport)
    #endif
    
    extern "C" API  void HelloWorld();
    
    cpp:
    #include"Header.h"
    void HelloWorld()
    {
        std::cout << "Hello, World!" << std::endl;
    }
    
    

    main.cpp

    #include <windows.h>
    #include <iostream>
    #include"../.../Header.h"
    
    
    typedef void (*HelloWorldFunc)();
    
    int main() {
      
        HMODULE hDll = LoadLibraryA("mylib.dll");//Put the dll and cpp in the same folder
        if (hDll != NULL) {
         
            HelloWorldFunc helloWorld = reinterpret_cast<HelloWorldFunc>(GetProcAddress(hDll, "HelloWorld"));
            if (helloWorld != nullptr) {
               
                helloWorld();
            }
            else {
                std::cout << "Failed to retrieve function address." << std::endl;
            }
            getchar();
           
            FreeLibrary(hDll);
        }
        else {
            std::cout << "Failed to load the DLL." << std::endl;
        }
    
        return 0;
    }
    
    
    

    And g++ command:g++ main.cpp

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

  2. Phan Hoài Sơn 40 Reputation points
    2023-06-27T08:43:23.5333333+00:00

    I try with your suggest, without -L... -l
    When I run build , I got new error not found file hello.h (see image attached)

    User's image

    User's image

    I try with your suggest, without -L... -l
    When I run build , I got new error not found file hello.h (see image attached)

    User's image

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.