cmakeminimumrequired(VERSION 3.18) project(Main) addlibrary(lib SHARED test.cpp)…
- User
cmake_minimum_required(VERSION 3.18) project(Main) add_library(lib SHARED test.cpp) add_executable(Main main.cpp)
Error loading library: %1 nÆest pas une application Win32 valide.
modify the cmake to handle this eroor
- Assistant
To handle the error "Error loading library: %1 nÆest pas une application Win32 valide", you can try modifying your CMakeLists.txt file as follows:
cmake_minimum_required(VERSION 3.18) project(Main) # Add a condition to check the operating system type if(WIN32) # On Windows, add the .lib file instead of the .dll file for the library add_library(lib STATIC test.cpp) else() # On non-Windows systems, add the library as a shared object (.so or .dylib) add_library(lib SHARED test.cpp) endif() add_executable(Main main.cpp) # Link the library to the executable target_link_libraries(Main lib)Explanation:
- The error "Error loading library: %1 nÆest pas une application Win32 valide" suggests that the library you are trying to load is not a valid Win32 application.
- One potential reason for this error could be that you are trying to load a shared library (.dll) on a non-Windows system.
- By using the
if(WIN32)condition in the CMakeLists.txt file, we check if the current system is Windows. If it is, we add the library as a static library (.lib) that can be linked directly with the executable. If it is not, we add the library as a shared library (.so or .dylib) that can be loaded dynamically at runtime.
- User
simple main to load a library on windows (the library called test.lib
- Assistant
To load a library called test.lib in a simple main program on Windows, you can use the following code:
#include <iostream> #include <Windows.h> int main() { // Load the library HINSTANCE hLibrary = LoadLibrary("test.lib"); if (hLibrary != NULL) { // Library loaded successfully // Get the function address from the library FARPROC function = GetProcAddress(hLibrary, "function_name"); if (function != NULL) { // Function found in the library // Call the function (*function)(); // Do any other operations with the library functions // ... } else { // Function not found in the library std::cout << "Failed to find function in library." << std::endl; } // Unload the library FreeLibrary(hLibrary); } else { // Failed to load the library std::cout << "Failed to load library." << std::endl; } return 0; }Note: Replace
function_namewith the actual function name you want to call from the test.lib library. Make sure test.lib is in the same directory as your main.cpp file or provide the full path to the test.lib file in the function. - User
add some error handling messages
- Assistant
Certainly! Here's an updated version of the code with additional error handling messages:
#include <iostream> #include <Windows.h> int main() { // Load the library HINSTANCE hLibrary = LoadLibrary("test.lib"); if (hLibrary != NULL) { // Library loaded successfully // Get the function address from the library FARPROC function = GetProcAddress(hLibrary, "function_name"); if (function != NULL) { // Function found in the library // Call the function (*function)(); // Do any other operations with the library functions // ... } else { // Function not found in the library std::cout << "Failed to find function in library." << std::endl; } // Unload the library FreeLibrary(hLibrary); } else { // Failed to load the library DWORD errorMessageID = GetLastError(); LPSTR messageBuffer = nullptr; size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); std::cout << "Failed to load library. Error code: " << errorMessageID << std::endl; if (messageBuffer != nullptr) { // Print the error message std::cout << "Error message: " << messageBuffer << std::endl; // Free the buffer LocalFree(messageBuffer); } } return 0; }