qpdf icon indicating copy to clipboard operation
qpdf copied to clipboard

vs2017 getAllPages() exception

Open logan-x opened this issue 8 years ago • 36 comments

I copied the qpdf.cc file in my test project,Then input the command line:d:\2.pdf d:\out.pdf --pages d:\2.pdf 8-15 -- When I'm running here : orig_pages(qpdf->getAllPages()),there is an exception. And then I tested it, and every time I called "qpdf->getAllPages()", it triggered an exception. image Oddly enough, this is not the case when I run the command line directly. qpdf d:\2.pdf d:\out.pdf --pages d:\2.pdf 8-15 --

in debug mode.

logan-x avatar Mar 27 '18 04:03 logan-x

This is probably caused by an STL structure being passed between modules using two different versions of C runtime. The getAllPages method returns an STL vector created in you QPDF dll and it is used in your example application. My guess is that these two modules, the dll and the program, are using two different versions of C runtime -- that is, they are built with different versions of Visual Studio. I think many other libraries avoid using STL in their API. But for the libraries that do use STL, you need to make sure that the library is compiled with the same Visual Studio version as the modules that use it (or if possible just not use the API parts that use STL). What is your view on this, @jberkenbilt ? Have you considered removing STL from the API or maybe provide an alternative page API? See also discussion here, for instance: https://social.msdn.microsoft.com/Forums/vstudio/en-US/8f2843ae-010d-4440-ad1b-9f021bb74c2e/stl-compatibility-across-different-versions-of-crt?forum=vcgeneral .

mtoftdal avatar May 22 '18 12:05 mtoftdal

Removing STL from qpdf's interface is a non-starter. It's a deep part of the interface. I'm not really a Windows developer, but I can look at the link you provided. People are always free to download qpdf and build from source. If there's anything I can update in the docs or do differently about how I build or distribute the msvc binaries, I'm certainly happy to consider, but STL is not going anywhere.

jberkenbilt avatar May 22 '18 15:05 jberkenbilt

That's totally fine with me, @jberkenbilt. :-) I was just curious to know if you had considered this. As far as I know this is not just a Windows problem, though it's much more obvious there. Other platforms can have incompatible STL libraries too, libstdc++ and stdc++ on Mac and Linux are probably the most relevant here.

mtoftdal avatar May 22 '18 18:05 mtoftdal

...and for the second part of your comment, I don't think you can address this in the build. But it might be helpful to state in the docs that the same MSVC version must be used for building the DLL and for building the program using the DLL (at least if the STL part of the API is going to be used). And for the current pre-built DLLs that requires VS 2015 as far as I can tell.

mtoftdal avatar May 23 '18 07:05 mtoftdal

I'm facing the same issue and would prefer to avoid rebuilding qpdf lib, at least at this stage of evaluating/writing sample programs. I will be glad to do it later [aka updatee for Visual Studio 2018] when I will move to use this lib in an actual product ;-)

As a suggested simple solution, would it be possible to reference/include MSVC DLLs used for the qpdf lib that is included in the release?

alexis-idigo avatar Feb 23 '19 10:02 alexis-idigo

btw, I installed toolset 140 and got same issue as originally reported.

Please note I think qpdf is the best tool ever for manipulating PDF thus my push to be able to use it ;-)

alexis-idigo avatar Feb 23 '19 15:02 alexis-idigo

More precisely, both using toolset 140 and 141, I can't link using the non-debug multi-threaded LL [since I expect as indicated in the documnetaion that qpdf lib would use the release version] due to these errors: pdf-count-strings.obj : error LNK2019: unresolved external symbol __imp__invalid_parameter referenced in function "public: class QPDFPageObjectHelper const & __cdecl std::_Vector_const_iterator<class std::_Vector_val<struct std::_Simple_types > >::operator*(void)const " (??D?$_Vector_const_iterator@V?$_Vector_val@U?$_Simple_types@VQPDFPageObjectHelper@@@std@@@std@@@std@@QEBAAEBVQPDFPageObjectHelper@@XZ) pdf-count-strings.obj : error LNK2019: unresolved external symbol __imp__CrtDbgReportW referenced in function "public: class QPDFPageObjectHelper const & __cdecl std::_Vector_const_iterator<class std::_Vector_val<struct std::_Simple_types > >::operator*(void)const " (??D?$_Vector_const_iterator@V?$_Vector_val@U?$_Simple_types@VQPDFPageObjectHelper@@@std@@@std@@@std@@QEBAAEBVQPDFPageObjectHelper@@XZ)

alexis-idigo avatar Feb 23 '19 15:02 alexis-idigo

I moved on and tried to build the library. Followed instructions at "https://github.com/qpdf/qpdf/edit/master/README-windows.md" Successful until I reached thsi step: Configure and build as follows:

./config-msvc
make

For which I got: alexis@Butterfly MINGW64 ~

./config-msvc

-bash: ./config-msvc: No such file or directory

Hoping to get some help ;-)

alexis-idigo avatar Feb 23 '19 15:02 alexis-idigo

Update: I managed to rebuild qpdf and qpfd lib using VS2015 x64 I just had to:

  • move the external libs inside a subfolder of qpdf-master folder named "external-libs".
  • set the path into VS console first to qpdf-master folder before launching MSYS2 console Except for these minor installation details, I did not have to edit any file and qpdf seems to be working based on simple test

I will now:

  • try to build and run the full test suite on this first
  • redo the build with VS2017

Before moving on to change the config to also build a debug version of the lib ["qpdfd.lib"?] as part of the process.

Will keep posted in case this is useful for others...

alexis-idigo avatar Feb 24 '19 08:02 alexis-idigo

Just wanted to give a quick update: I managed to build libqpdf for MSVC 2015 in debug mode and seems to be working properly on a few examples ;-)

I also run the test suite [without image comparison] and just get 4 fails, seemed related to encryption: [...] qpdf 1495 (encrypt R3,V2,U=view,O=master) ... FAILED qpdf 1496 (check /P) ... FAILED [...] qpdf 1500 (check /P) ... FAILED qpdf 1690 (copy of unfilterable with crypt) ... FAILED

I will continue and proceed with:

  • MSVC 2017 support
  • Image Comparison test suite

Hopefully I can reach something good enough in the next few days to put a pull request ;-) Changes up to now are minor and mostly related to the build mechanism to support various platforms [x86, x64] without any warning and mode [debug/release] that I should complement to support various tool chains [v140, v141].

I'm lacking experience with make and related tools so taking slightly longer and will probably need some "tolerance" and help from the experienced people here to make my pull request as clean as they would like it to be ;-)

alexis-idigo avatar Feb 24 '19 21:02 alexis-idigo

PS, when I said "I managed to build libqpdf for MSVC 2015" it was actually using MSVC 2017 but with proect set up to use vc140 tool chain so that's a really good sign imho

alexis-idigo avatar Feb 24 '19 21:02 alexis-idigo

@alexis-idigo Thanks for your work on this. I'm happy to tweak whatever you come up with to handle make issues or other things. Mostly as long as what you produce doesn't break other stuff, I'm inclined to take it. Anything that helps with building qpdf on multiple environments is appreciated. There are some other pull requests I have yet to study that get qpdf working in some other compilers, and I have one commercial user who builds qpdf on over 100 permutations of versions of compilers and operating systems with good success using his own build system. As such, I suspect the issues to be limited to build and not qpdf's actual code. Anyway, thanks for your work and continual updates to this ticket with your progress. Sorry I am not able to provide direct assistance.

jberkenbilt avatar Feb 24 '19 23:02 jberkenbilt

@jberkenbilt Thanks for your kind comments! I'm hoping to not only make it work on various MSVG but also to fix the FAILED test, and come with changes that would not break the build. I agree with you, should be mostly in the build, except for the FAILED, I found some strange and presumably erroneous use of char* vs unicode string in calling Windows API and I suspect they are the reasons. Will submit a clean pull request when I'm ready. For now directly patching my local branch using a MSVC project with all files statically linked for faster test ;-)

alexis-idigo avatar Feb 26 '19 08:02 alexis-idigo

@alexis-idigo I don't know if this is still an issue for you, but recent qpdf releases have made some improvements to Unicode handling on Windows, and current unreleased master has a number of other fixes around casting. If you're subscribed to the qpdf-annonce list, I'll be announcing an alpha of qpdf 9 for testing soon. If this is no longer an issue, we can close this, or we can close it if you're going to do other investigation and create a new issue or pull request.

jberkenbilt avatar Jun 22 '19 15:06 jberkenbilt

As I commented in #427, I was able to get Debug mode to work OK (using QPDF binaries) by changing the Runtime Library option, from the Debug default [Multi-threaded Debug DLL (/MDd)] to the Release default [Multi-threaded DLL (/MD)]. As I'm using QPDF in a DLL, I also had to change to /MD in my application, since I was passing strings from the application to the DLL.

drcdr avatar May 06 '20 16:05 drcdr

So, fundamental problem is that with MSVC, there are four important C++ runtimes, which are incompatible:

  • single-threaded, debug
  • single-threaded, release
  • multi-threaded, debug
  • multi-threaded, release

qpdf appears to be built with the multi-threaded release - if you use any other one, many things are incompatible, including std::vector. QPDF's windows zip also includes some runtime DLLs; if those are needed, it would be good to upgrade to the UCRT - then, there is no need to include them, and VS2015-VS2022 can all use the same version (but still need the same threading + debug/release variant)

There's historically two approaches:

  • always build your project and all your other dependencies with multi-threaded, release
  • build yourself with MSVC + msys in the configuration you want

Option 1 makes many issues (whether in qpdf usage or not) much harder to debug; option 2 is hard to integrate with other environments.

So, I took a look at building just libqpdf as a static library with cmake - my project already uses cmake, but for projects that don't use cmake, it should be easier to integrate than msys as visual studio nowadays includes cmake.

Turns out it's pretty straightforward: https://github.com/fredemmott/OpenKneeboard/blob/683801599e507e9e47aa13f71758c99ee0efa6c0/third-party/qpdf.cmake - include that in your cmake project and depend on the qpdf target. It will use the same release/debug and single/multithreaded runtime as the rest of your project.

If you're not using cmake in your project, you need a small top-level wrapper cmake file, and set MSVC_RUNTIME_LIBRARY and CMAKE_BUILD_TYPE to match the configuration you want.

You also need qpdf-config.h.in from the same directory, and zlib and libjpegTurbo targets defined - likely also want to grab libjpeg-turbo.cmake and zlib.cmake from the same place.

Caveats:

  • only tested with 64-bit Visual Studio 2022
  • always uses native crypto. See QPDF's readme for downsides. This is fine for me as all I care about is extracting outlines for unencrypted and trusted PDFs, your use case may differ
  • always builds as a static library

If the maintainer would be interested in merging CMake support for libqpdf, I can look at addressing these limitations and making a PR (including extending to linux and macos).

fredemmott avatar Feb 09 '22 16:02 fredemmott

@fredemmott I have considered switching over to cmake and might be interested in looking into what it would take. I do not know cmake, but I have extensive build experience having actually built and maintained a build system about 15 years ago, so I don't think I'll have any trouble understanding it. I'm not really comfortable with Windows toolchains. What qpdf has is something I worked out over 10 years ago and have kept working, but switching over to cmake would probably be a good thing. If you're interested in helping with getting qpdf integrated with cmake, let's create a separate issue (or discussion) and work it out before going to a pull request. Note that qpdf 10.6, just released today, now invokes a Python script to do some code generation. I ship the generated files, and the code generator is only run when --enable-maintainer-mode is passed to ./configure, but I would need that to work. There is also a lot of custom stuff in my autoconf setup. qpdf has its own make system (based on my previous build work). It uses autoconf but not the whole autotools toolchain.

jberkenbilt avatar Feb 09 '22 17:02 jberkenbilt

@fredemmott I've pretty much decided to switch the build over to cmake for qpdf 11. It'll be a few months, but I'm motivated by this, the need to upgrade to a newer Windows toolchain again, and some issues with rpath on the Linux build. cmake has definitely gained a lot of popularity, and I'd rather delegate to them when it comes to keeping up with all the different options for building on different platforms. A lot of the stuff in autoconf is probably overcome by events with the decision to standardize on C++14, so it will give me a chance to take a top-to-bottom look on the whole build process. The build is actually quite straightforward....

jberkenbilt avatar Feb 18 '22 21:02 jberkenbilt

@fredemmott I'm not quite there yet, but I've started working on getting qpdf's build working with cmake. I have pretty deep build experience in the UNIX world, but I'm new to cmake and I have no IDE development experience in Windows. Would you be willing to help out when it comes to testing the cmake in your Windows environment? There are lots of things in cmake that appear to be there to support IDEs, and I would have no way to actually notice if I got them wrong. If you're willing, then I will open a "discussion" issue and tag you on it when I get a little closer. My first goal is to get the cmake build to have parity with the existing autoconf-based build on Linux, then mac, then Windows from the CLI. Then I'll be ready for help. I might be ready for help during the Windows/CLI part. I'm chipping away at this in my available time. It will probably be a few weeks before I've gotten that far, and it will probably be a few months before an official qpdf release uses cmake. Please let me know if you're willing to help out. If so, great. If not, I'll see if I can find someone who is a Windows developer who might be able to help.

jberkenbilt avatar Feb 26 '22 21:02 jberkenbilt

Sure, though I've actually never needed any of the IDE-specific options in CMake; though, some MSVC options refer to the compiler, not the IDE. My usual workflow is VSCode with the CMake extension - no special support in the CMake files - and in my cross-platform projects this works pretty much identically between Windows, MacOS, and Linux.

Microsoft also make a Windows 11 development VM image available at no cost at https://developer.microsoft.com/en-us/windows/downloads/virtual-machines/

fredemmott avatar Feb 27 '22 17:02 fredemmott