Build FFTW lib with Visual Studio 2015 [added: steps for VS 2019]

浪子不回头ぞ 提交于 2020-01-04 06:29:11

问题


I'm trying to use Visual Studio 2015 to compile a project that uses FFTW. Unfortunately, the precompiled binaries from the FFTW website aren't compatible with VS 2015, due to this problem: unresolved external symbol __imp__fprintf and __imp____iob_func, SDL2. You get a link error when you try to compile.

I'm looking for any advice on how I can compile a compatible version. The obvious answer would be to recompile FFTW with VS 2015, but I can't find any instructions on how to compile it with Visual Studio at all, and lots of suggestions that it would be hard to do that. The standard version was compiled with MinGW, and they do provide instructions on how to do that. But can MinGW produce a library compatible with VS 2015? I haven't found any information about that either.


回答1:


In case anyone needs to build a static or dynamic FFTW library for a Windows program using Visual Studio 2019:

You do NOT actually need to do any of the manual and complex steps in the accepted answer -- they are error prone and a hundred times longer than the following steps.

First of all, I've got good news for you -- Visual Studio 2019 (both the free Community and Professional versions) supports CMake! So you only need to do these steps:

1) If you haven't installed all components supplied with Visual Studio 2019, launch it, press Ctrl-Q, and type "cmake tools for linux". It should find the installation option, which you click and install the component. If you aren't offline, launch the installer of VS and select this optional component ("CMake tools for Linux") for installation.

2) Create an empty solution in Visual Studio 2019 choosing a template suitable for libraries, C++, and Windows Desktop development.

3) When the solution is created, select your desired target platform (such as x64 Release) and build type (Release/Debug) as usual. Quit VS.

4) Download the latest source archive from: http://www.fftw.org and extract the files to the folder where you created your VS solution and project.

Do NOT run any of the supplied config scripts under MinGW, and do NOT do any other of the steps you are instructed to do in the official or third-party docs.

5) Run Visual Studio, open the solution you created, and in the Solution Explorer subwindow, click the icon with the tooltip of 'Switch View' and use it to switch to the 'Folder View' mode.

As soon as that happens, VS starts to populate Solution Explorer with all the files, and most importantly, it automatically discovers the config scripts and CMake makefiles and starts configuring the solution including the critically important config.h.

6) In the Solution Explorer subwindow, click the icon with the tooltip of 'Switch View' icon again and use it to switch to the 'CMake Targets View'.

7) Right-click the project name in the Solution Explorer subwindow and select 'CMake Settings'.

8) In the CMake Settings subwindow, set the 'Configuration type' to either Debug or Release and in the 'Toolset' field select either x64 or x86, etc.

9) In the CMake Settings subwindow click the link "Save and generate CMake cache to load variables".

When the process finishes, click the link a second time (sometimes this is needed, especially if changing the build target/type).

10) If you want to build a static .lib file, then, in the CMake Settings subwindow, disable the option BUILD_SHARED_LIBS. Otherwise, you will build a shared .dll.

11) In VS, select Build > Rebuild All and your .lib and .dll files should be in the subfolder \out\build\x64-Debug\ (for x64 Debug build of course -- other targets will be somewhere else).

If it failed, sometimes it's just failing build of the benchmarking app. Fortunately, you can disable its building in the CMake Settings subwindow easily -- just disable the option BUILD_TESTS.

12) Add the resultant .lib or .dll file to your main application and/or its source code (in the VS project settings: Linker > General > 'Additional Library Directories' > path to the .lib file; and also in Linker > Input > 'Additional Dependencies' the .lib filename).

IMPORTANT: When you close the VS solution and then open it again, you have to repeat steps 5 and 6. Otherwise, nothing would be built if you used 'Rebuild All'.

And final technical note: If your main application links to the C Run-time Library (CRT) statically, rather than dynamically as a DLL, you will also need to make the following replacements in all files named 'CMakeCache.txt' you find in the 'out' subfolder: replace all /MDd by /MTd and all /MD by /MT. But you should do that only after you've created all the build configs (debug/release, x86/x64). After you make the replacements, open CMake settings in VS again and click the link 'Save and generate CMake cache to load variables' again.

Hope this helped. Enjoy!




回答2:


I eventually managed to figure this out myself. I don't promise I did it in the best way, since I'm not a Visual Studio expert, but it did work. So for the benefit of posterity, here's what I did.

First, duplicate config.h.in. Call the new copy config.h. Now edit it by hand to set options for the library you're going to generate: what precision mode it should use, which vector instruction sets (if any) to compile for, the sizes of various types (which will depend on whether you plan to compile in 32 or 64 bit mode), whether to include threading support, etc.

Now create an empty VS project. I recommend putting it at the top level of the source folder. Then in the Solution Explorer, click the "Show All Files" icon at the top. This will make the following steps easier, since you can add or exclude files directly from the Solution Explorer.

Bring up the Project Properties window. There's a lot of options you'll need to set:

In General, set "Configuration Type" to static library. (Or you can set it to dynamic library if you want a DLL, but then you'll need to figure out how to get it to export the necessary symbols. I didn't do that, since I wanted a static library.)

In VC++ Directories, edit "Include Directories". Add the following directories (replacing "(source dir)" by the actual directory containing the source):

(source dir)
(source dir)\api
(source dir)\dft
(source dir)\rdft
(source dir)\reodft
(source dir)\kernel
(source dir)\simd-support
(source dir)\dft\simd
(source dir)\dft\scalar
(source dir)\rdft\simd
(source dir)\rdft\scalar

If you're including threading support, then in C/C++ > Language, set "Open MP Support" to yes.

If you're including AVX support, then in C/C++ > Code Generation, set "Enable Enhanced Instruction Set" to AVX. This means the library you generate will not work on any computer that doesn't support AVX. You might be able to get around that restriction by only setting this option on the individual source files that use AVX. I'm not sure about that.

In C/C++ > Output Files, set "Object File Name" to $(IntDir)/%(RelativeDir)/. (FFTW has a lot of files with the same names but in different directories, and by default VS puts all object files in the same directory, which won't work.)

Now you're ready to add source files to the project, which you can do by right clicking in the Solution Explorer and choosing "Include In Project". You should add all .c files in the following folders:

api
dft
dft/scalar
kernel
rdft
rdft/scalar
reodft

If you want threading support, you should add all source files in "threads" except for threads.c. (That one contains the pthreads version.)

If you want support for any vector instruction sets, you should include only the files relating to the particular instruction sets you want from the following folders:

dft/simd
rdft/simd
simd-support

Presumably if you want mpi support, you include the files in "mpi". I didn't do that, so I don't know if there's anything else you'll need to make that work.

Be sure to set the configuration to "Release" and either "x86" or "x64", depending on what type of binary you want.

And now, if I haven't forgotten anything critical, you should be able to compile a library.



来源:https://stackoverflow.com/questions/35517892/build-fftw-lib-with-visual-studio-2015-added-steps-for-vs-2019

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!