How to import a c++ module from another library? - c++

I've got a c++ project called core that compiles successfully and produces a dll and a lib file when compiled on Windows. Now I need to import functionality from core in another project called main. core has a file called core.module.ifc:
export module core
export import :mod
export import :egx
export import :utils
In my main project, I have a single demo.cpp which looks like this:
#include "someOtherLib.h"
import std.core // error occurs here
import core
.....
some other code
However, main does not compile with error:
1>C:\Users\main\Desktop\Projects\demo\src\demo.cpp(8,11): error C2230: could not find module 'core'
I am using VS 16 2019 to compile, with std::c++latest and platform toolset v142. The core.lib file is correctly given as input to the linker in the project's properties. From what I understand, the compiler has no way of knowing that core is an outside library and looks for export module core in the demo project (which obviously fails) and requires a file that has all the declarations of the core lib. Am I correct on this assumption? If so, how would this file look?
So I believe a summary of my question would be, how do I import a module that is exported from a library into my project?

Related

Fail to import QML module using CMake

I'm currently building a minimalist app following this CMake architecture:
-root
--QmlModule
---Component1.qml
---Component2.qml
--App1
---main.cpp
---main.qml
--App2
---main.cpp
---main.qml
I use "qt6_add_qml_module" to create a QML module at "QmlModule" level as a STATIC library.
qt_add_library(myComponentTarget STATIC)
qt6_add_qml_module(myComponentTarget
URI QmlModule
VERSION 1.0
QML_FILES
Component1.qml
Component2.qml
RESOURCES
logo.png)
Then, at App1 (and App2) level, a link to the module is done using "target_link_libraries". "qt6_add_qml_module" does some work behind the scenes in order to expose the module trough an automatically generated plugin named "your_component_URIplugin". More details about this here.
add_executable(App1Exe
main.cpp)
qt6_add_qml_module(App1Exe
URI App1
VERSION 1.0
QML_FILES
main.qml)
target_link_libraries(App1Exe
PRIVATE
myComponentURIplugin)
At Root level, I overload QML_IMPORT_PATH in order to link to the build folder and add all subdirectories.
set(QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qmlModule)
add_subdirectory(QmlModule)
add_subdirectory(App1)
add_subdirectory(App2)
I run CMake without any errors, and open App1/main.qml file.
On my import QmlModule, the module can't be found:
module "lupinComponentsplugin" is not installed
How to make my module visible from my Apps ?
What step am I missing ?
I'm currently doing something similar.
I have created a demo app where I import modules. The modules provide QML and C++ items to the main app. Check the comments in the CMAKE files to find out how this works.
Here is the link:
https://gitlab.com/basic53/DemoApp
Feel free to comment on this.
Another tip: If qt_add_qml_module is not working properly, sometimes it is necessary to remove the whole build folder and update the QML code model. You can check the generated files to see, if your plugin has been created with all its types.
CMake itself was fine, this was a runtime error and not a link error.
This issue was raised because the QQmlApplicationEngine wasn't finding path towards my module's QMLDIR.
In the end, the only thing missing was an additional import path ":/" in QQmlEngine:
QQmlApplicationEngine engine;
engine.addImportPath(":/");

D language import local module

I am new to D language. i have a project A. When i executed dub build it has created me libA.so. i am trying to create a different .d file and import this module.
now i wanted to write a D wrapper to consume it.
when i tried
import a
it did not work. i copied the libA.so to the sudo cp libA.so /usr/include/dmd/druntime/import/ but the import did not work.
Can you please help me how can i import this ?
The official documentation for writing and using shared libraries in D on Linux is here:
https://dlang.org/articles/dll-linux.html
You don't import shared libraries in D. You import modules (source files). So the .so file does not belong in the import directory. You need to link to that. import a means the compiler will search the import path for a.d. That's what needs to be in your import directory. If your library has multiple source files, they each need to be on the import path and imported separately.
The question is a bit unclear, but it sounds like you want to create a library via dub, and then use that library in another project. Is that correct?
For example, your project may have a structure like:
myProject
├- commonLib
|
├- program1
| (depends on commonLib)
└- program2
(depends on commonLib)
If this is what you are trying to do, you should consider using Dub sub-packages.
For example, myProject/dub.sdl might look like this:
name "myProject"
targetType "none"
dependency "myProject:common" path="."
dependency "myProject:program1" path="."
dependency "myProject:program2" path="."
subPackage "./common/"
subPackage "./program1/"
subPackage "./program2/"
Your common library at myProject/common/dub.sdl may look like this:
name "common"
targetType "library"
And finally you would depend on it like this in myProject/program1/dub.sdl:
name "program1"
targetType "executable"
dependency "myProject:common" path="../"

Can not import submodule

On clang version 6.0.0 (tags/RELEASE_600/final) I can not import std submodules like std.vector. Whole import std works fine, but import std.vector not. I'm using libc++ modulemap which define this module properly.
Edit
Same problem exists with custom modulemap
module test {
explicit module sub {
header "test.hpp"
export *
}
}
It can not load module test.sub but reports that symbol foo could be find in it.
Edit 2
clang 5.0.2 behaves in same way.
Edit 3
6.0.1-rc1 same
Is there any issue releated to that or infirmation that it is not yet supported?
For import modules or sub-modules, clang 6 does not work perfectly everywhere, clang 7 will be improved in the future.
You can try to set -fmodules-cache-path=<your-cache-path> flag explicitly, and you can see that, if there is some module involved the build, clang will populate the pre-compiled module files (normally *.pcm) to it when it builds.
In clang 7 documentation, you can practice Module Map Language to create your own modules to include some headers and export them, then import your own modules. As the documentation described, it is not stable now. You need to try.
At least for this moment you can use import std as a temporary workaround.

Write unittest and save in the separate folder

I am trying to write a unittest for my class. First of all, i install the package unittest and create a spec folder to save all my unittest files. Then i create a test dart file with following contents:
library i18n_test;
import 'package:unittest/unittest.dart';
void main() {
}
When i run the file, i got the following error
Unable to open file: D:\Dart\i18n\bin\spec\packages\unittest\unittest.dart'file:///D:/Dart/i18n/bin/spec/i18n_test.dart': error: line 3 pos 1: library handler failed
import 'package:unittest/unittest.dart';
As i mentioned, i saved my unittest file in the spec folder, i think that because the compiler could not find the package unittest.
My Folder structure looks like:
What do i wrong?
The error is caused by a missing packages symlink in your spec folder.
see https://code.google.com/p/dart/issues/detail?id=16947
Anyway
unit tests should be put into the test folder (i18n/test).
This way your test code won't be included in the build output.
It's best to have most of your code in the lib folder to make it easy to import into your tests using package imports like:
import 'package:i18n/my_dart_file.dart';
you should still be able to import from bin or web using relative imports like
import '../bin/my_dart_file.dart';
but that form is not very refactoring-friendly.

building boost python examples using Visual Studio 2008

I'm using Boost Python library to create python extensions to my C++ code. I'd like to be able to invoke from python the 'greet' function from the C++ code shown below:
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
And the python code :
import hello_ext
print hello_ext.greet()
I've managed to do this using the bjam (hello_ext.pyd is generated and it works nice), but now I'd like to build it using Visual Studio 2008. A hello.dll gets built (but neither hello_ext.dll nor any .pyd). After invoking my python code I get an error:
ImportError: No module named hello_ext.
After renaming the hello.dll to hello.pyd or hello_ext.pyd, I get another ImportError: Dll load failed
How can I build the correct .pyd file using VS 2008?
Firstly, make sure you only try to import the release version from Python; importing the debug version will fail because the runtime library versions don't match. I also change my project properties so that the release version outputs a .pyd file:
Properties >> Linker >> Output:
$(OutDir)\$(ProjectName).pyd
(I also create a post-build action to run unit tests from python)
Next, make sure you define the following in your stdafx.h file:
#define BOOST_PYTHON_STATIC_LIB
Lastly, if you have more than one python version installed, make sure that you're importing the right version of python.h (in Tools >> Options >> Projects and Solutions >> VC++ Directories >> Include Files).
The error ImportError: Dll load failed usually means that your .pyd module depends on other DLLs that couldn't be found - often msvc*.dll. You may want to try opening the .pyd file in Notepad and searching for ".dll". Then check if all of the DLL dependencies exist in your directory or PATH.
Or use Dependency Walker which will find missing dependencies for you
Even though this is a question issued several years ago(still not easy to find solution), but I meet the same problem today, and after hours searching, finally I found a feasible solution.
The reason is just as simple as which is noticed by #AndiDog, the .pyd file you build depends on some other .dll;
In my case, It's boost_python-vc120-mt-1_58.dll under the folder [C++ boost folder]/stage/lib/
So, what I do is to copy this file, and paste it under the .pyd file folder, and then my python can properly import the project I build .
maybe there are some other solutions, that is build your project not depend on dynamic library, use static library instead. some of the source said to define BOOST_PYTHON_STATIC_LIB in VS Preprocessor, then your project will not depend on dynamic library(I am a new C++er), but be sure you have build libboost_python-vcXXX-mt-1_58.dll in boost.
to define Preprocessor, the route is:C/C++->Preprocessor->Preprocessor Definitions->edit BOOST_PYTHON_STATIC_LIB
Please make sure you have flag -lpython26 (if you are using python2.6) and filename should be hello_ext.pyd in your case.