tbenthompson / cppimport

Import C++ files directly from Python!
MIT License
1.18k stars 67 forks source link

[Windows] Compilation warnings: unknown option '-std=c++11' #54

Closed kokounet closed 3 years ago

kokounet commented 3 years ago

Hello, first of all, great work on this tool! it works like a charm!

I'm a windows user, and the default example raises 2 warnings because of those flags I believe: https://github.com/tbenthompson/cppimport/blob/9b3326424c90e7023e34a1ca413221f6509049a3/cppimport/templating.py#L30

cl : Command line warning D9002 : ignoring unknown option '-std=c++11'
cl : Command line warning D9002 : ignoring unknown option '-fvisibility=hidden'

I think msvc has its own set of compiler flags (like /std:c++11) and using those on windows might fix those warnings. Thanks again for the good work!

tbenthompson commented 3 years ago

Thanks for this! I'll get this fixed soon. It's good to hear that the tool works with MSVC even if there are some rough edges.

kokounet commented 3 years ago

That's very cool! thank you for the quick response!

I also have an unresolved symbol linking error when trying to integrate some c++ library because it checks that all the compile units are built in the same mode (debug/release) which is apparently not the case when using cppimport but works fine using visual studio 😕 I'm not sure what to do about that, do you have any idea of what might cause the issue?

tbenthompson commented 3 years ago

I have no idea. That's not an issue I'm familiar with. But, if you can copy-paste the error message, I'll let you know if I have any ideas...

kokounet commented 3 years ago

For the following code juce.cpp, where JuceHeader.h is in JuceLibraryCode/:

#include <pybind11/pybind11.h>
#include "JuceHeader.h"

PYBIND11_MODULE(juce, m) {
    m.def("add", [](int a, int b) { return a + b; });
}

/*
<%
cfg['include_dirs'] = ['JuceLibraryCode', 'JuceLibraryCode/modules']
setup_pybind11(cfg)
%>
*/

I obtain the following verbose output:

Missing trailer tag
Compiling C:\Users\Chris\devel\playground\smoothy\juce.cpp
running build_ext
building 'juce' extension
creating C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release
creating C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users
creating C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris
creating C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel
creating C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground
creating C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground\smoothy
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\Chris\devel\playground\smoothy\JuceLibraryCode -IC:\Users\Chris\devel\playground\smoothy\JuceLibraryCode/modules -IC:\Users\Chris\devel\playground\smoothy\.venv\lib\site-packages\pybind11\include -IC:\Users\Chris\devel\playground\smoothy\.venv\lib\site-packages\pybind11\include -IC:\Users\Chris\devel\playground\smoothy -IC:\Users\Chris\devel\playground\smoothy\.venv\include -IC:\Program Files\Python39\include -IC:\Program Files\Python39\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\ATLMFC\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt /EHsc /TpC:\Users\Chris\devel\playground\smoothy\.rendered.juce.cpp /FoC:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground\smoothy\.rendered.juce.obj -std=c++11 -fvisibility=hidden
cl : Command line warning D9002 : ignoring unknown option '-std=c++11'
cl : Command line warning D9002 : ignoring unknown option '-fvisibility=hidden'
.rendered.juce.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Users\Chris\devel\playground\smoothy\.venv\libs /LIBPATH:C:\Program Files\Python39\libs /LIBPATH:C:\Program Files\Python39 /LIBPATH:C:\Users\Chris\devel\playground\smoothy\.venv\PCbuild\amd64 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\ATLMFC\lib\x64 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64 /EXPORT:PyInit_juce C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground\smoothy\.rendered.juce.obj /OUT:C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\juce.cp39-win_amd64.pyd /IMPLIB:C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground\smoothy\juce.cp39-win_amd64.lib
   Creating library C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground\smoothy\juce.cp39-win_amd64.lib and object C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\Release\Users\Chris\devel\playground\smoothy\juce.cp39-win_amd64.exp
.rendered.juce.obj : error LNK2001: unresolved external symbol "public: __cdecl juce::Colour::Colour(unsigned int)" (??0Colour@juce@@QEAA@I@Z)
.rendered.juce.obj : error LNK2001: unresolved external symbol "public: __cdecl juce::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_release_mode::this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_release_mode(void)" (??0this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_release_mode@juce@@QEAA@XZ)
C:\Users\Chris\AppData\Local\Temp\tmpbzdzycvz\juce.cp39-win_amd64.pyd : fatal error LNK1120: 2 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX86\\x64\\link.exe' failed with exit code 1120

there is this option /DNDEBUG during compilation that may cause the problem ? there is a comment in the JUCE code for this error:

As the very long class names here try to explain, the purpose of this code is to cause a linker error if not all of your compile units are consistent in the options that they enable before including JUCE headers. The reason this is important is that if you have two cpp files, and one includes the juce headers with debug enabled, and another does so without that, then each will be generating code with different class layouts, and you'll get subtle and hard-to-track-down memory corruption!

Maybe I'm also not using your library correctly, that's also a possibility 😅, anyway thank you for your time!

tbenthompson commented 3 years ago

Huh, are you sure you're linking in the JUCE library at all? You may need to add something like

cfg['include_dirs'] = ...
cfg['libraries'] = ['juce'] 

Or whatever the appropriate library name would be. Generally, C++ libraries have headers that needed to included at compile-time and libraries that need to be linked at link-time. In general, I'd suggest you start by getting a simple single C++ file compiling with the library before adding complexity like Python bindings. That'll also help you understand the various compiler and linker flags and get a better scaffolding for debugging these kinds of problems in the future.

I'm going to close this since I'm pretty sure cppimport isn't causing your issues. Hope you resolve your problem!

kokounet commented 3 years ago

Hello again!

Thank you again for your comment, I managed to make it work after all! It was indeed not cppimport causing the issue. I needed to add information on how to compile the library and link to a bunch of system libraries as well!

now the config looks something like this:

<%
import glob
import sys
cfg["include_dirs"] = [
    "JuceLibraryCode",
    "JuceLibraryCode/modules",
    "JuceLibraryCode/modules/juce_audio_processors/format_types/VST3_SDK"
]
cfg["sources"] = sorted(glob.glob("JuceLibraryCode/*.cpp"))
if sys.platform == "win32":
    cfg["libraries"] = [
        "kernel32", "user32", "gdi32", "winspool", "comdlg32", "advapi32",
        "shell32", "ole32", "oleaut32", "uuid", "odbc32", "odbccp32",
    ]
elif sys.platform == "darwin":
    cfg["libraries"] = []
setup_pybind11(cfg)
%>