我有一個在 Windows 上使用 imagemagick 的簡單專案:
#include <Magick .h>
#include <iostream>
using namespace std;
using namespace Magick;
int main() {
cout << "Hello, World!" << endl;
string inputFileName =
"D:\\magick\\pic2.jpg";
string outputFileName = "D:\\magick\\pic_resized.png";
int width = 800;
int height = 600;
try{
Image image;
image.read(inputFileName);
Geometry geometry(width, height);
geometry.aspect(false);
image.resize(geometry);
image.write(outputFileName);
Blob b;
image.write(&b);
cout << "data size is " << b.length() << endl;
}
catch (...) {
cout << "error occurred";
}
return 0;
}
當我運行它時,我在該行得到一個例外image.read(inputFileName);:
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
我在網上讀到這個例外是由于缺少 dll,但我不明白這個缺少的 dll 是什么?
我正在使用 cmake 并且專案已經正常構建。
我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(demo-app)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
file(GLOB SOURCE_FILES src/*.cpp)
add_executable(demo ${SOURCE_FILES})
include_directories(src)
#ImageMagick
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
include_directories(deps/ImageMagick/include)
target_link_directories(demo PRIVATE deps/ImageMagick/lib)
target_link_libraries(demo
"CORE_RL_Magick _"
"CORE_RL_MagickCore_"
"CORE_RL_MagickWand_"
)
并且專案結構非常簡單:

這是我運行應用程式并收到錯誤時的日志(我強制它退出,因為它在例外中中斷):
-------------------------------------------------------------------
You may only use the C/C Extension for Visual Studio Code
with Visual Studio Code, Visual Studio or Visual Studio for Mac
software to help you develop and test your applications.
-------------------------------------------------------------------
Loaded 'D:\dev_haidar\c \demo_old\build\demo.exe'. Module was built without symbols.
Loaded 'C:\Windows\System32\ntdll.dll'.
Loaded 'C:\Windows\System32\kernel32.dll'.
Loaded 'C:\Program Files\Avast Software\Avast\aswhook.dll'.
Loaded 'C:\Windows\System32\KernelBase.dll'.
Loaded 'C:\Windows\System32\ucrtbase.dll'.
Loaded 'C:\Windows\System32\msvcp140.dll'.
Loaded 'C:\Windows\System32\vcruntime140.dll'.
Loaded 'C:\Windows\System32\vcruntime140_1.dll'.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_Magick _.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_MagickCore_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_MagickWand_.dll'. Module was built without symbols.
Loaded 'C:\Windows\System32\user32.dll'.
Loaded 'C:\Windows\System32\vcomp140.dll'.
Loaded 'C:\Windows\System32\win32u.dll'.
Loaded 'C:\Windows\System32\gdi32.dll'.
Loaded 'C:\Windows\System32\gdi32full.dll'.
Loaded 'C:\Windows\System32\msvcp_win.dll'.
Loaded 'C:\Windows\System32\advapi32.dll'.
Loaded 'C:\Windows\System32\msvcrt.dll'.
Loaded 'C:\Windows\System32\sechost.dll'.
Loaded 'C:\Windows\System32\rpcrt4.dll'.
Loaded 'C:\Windows\System32\ws2_32.dll'.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_freetype_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_bzlib_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_lcms_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_lqr_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_xml_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_zlib_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_harfbuzz_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_fribidi_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_glib_.dll'. Module was built without symbols.
Loaded 'C:\Windows\System32\shell32.dll'.
Loaded 'C:\Windows\System32\ole32.dll'.
Loaded 'C:\Windows\System32\combase.dll'.
Loaded 'C:\Windows\System32\dnsapi.dll'.
Loaded 'C:\Windows\System32\IPHLPAPI.DLL'.
Loaded 'C:\Windows\System32\imm32.dll'.
Loaded 'C:\Windows\System32\nsi.dll'.
Hello, World!
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
The program '[8620] demo.exe' has exited with code 0 (0x0).
有人可以告訴我如何解決這個問題嗎?
注意:include和lib下的檔案夾deps/ImageMagick是從官方網站安裝imagemagick ImageMagick-7.1.0-49-Q16-x64-dll.exe 復制/粘貼的。
uj5u.com熱心網友回復:
根據您鏈接到的網站上的檔案,建議的用法是:
請注意,在 Windows(可能還有 Mac)下,可能需要在使用 Magick 庫之前初始化 ImageMagick 庫。此初始化是通過將 ImageMagick DLL 的路徑(假設與您的程式位于同一目錄中)傳遞給 InitializeMagick() 函式呼叫來執行的。這通常通過提供程式的路徑來執行。
因此,您必須在使用它之前正確初始化該庫,以使您的程式正確運行。這個初始化可以像這樣完成:
int main(int argc, char* argv[]) {
InitializeMagick(*argv);
//Your code with Magick operations here.
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511322.html
標籤:C 图像魔术师
