JuliaInterop / Cxx.jl

The Julia C++ Interface
Other
757 stars 108 forks source link

Difficulty using Cxx inside a module #424

Closed goretkin closed 5 years ago

goretkin commented 5 years ago

I had done import Cxx twice in a project, and due to the line

https://github.com/JuliaInterop/Cxx.jl/blob/93359f1fe5a837c96047bfb102b70ba358e11d1e/src/Cxx.jl#L178

this resets the active instances. I personally found this behavior surprising, since all the translation units I had previously loaded were loaded into a different instance.

I thought I could keep a handle to the Cxx.CxxCore.ClangCompiler instance, so I could use it later. It seems that this definition

https://github.com/JuliaInterop/Cxx.jl/blob/93359f1fe5a837c96047bfb102b70ba358e11d1e/src/clanginstances.jl#L40

exists for that use.

So I tried my_cxx_compiler_instance = Cxx.instance(Cxx.__current_compiler__) right after all of my addHeaderDir and cxxinclude calls. And then later
__current_compiler__ = my_cxx_compiler_instance

However the definitions at https://github.com/JuliaInterop/Cxx.jl/blob/93359f1fe5a837c96047bfb102b70ba358e11d1e/src/codegen.jl#L840-L853 seem to restrict the type of the first argument to be CxxInstance unnecessarily.

goretkin commented 5 years ago

Actually, I'm not sure exactly what's happening. In the simple MWE below, I see __init__ called exactly once. reset_init! isn't called. In my larger project, I do see these functions called multiple times. In any case, I expect the test below to pass, but it does not.

https://github.com/goretkin/CxxInitMWE/blob/8c56237274d0668bcd1c3051c969387259c2e20f/test/runtests.jl#L7

MWE at https://github.com/goretkin/CxxInitMWE (archive attached for posterity) CxxInitMWE-master.zip

goretkin commented 5 years ago

My difficulties seem to have been related to precompilation and initialization. Adding __precompile__(false) to my module fixed my issue.

cdsousa commented 5 years ago

Related to https://github.com/JuliaInterop/Cxx.jl/issues/343