How to link boost library as dynamic library instead of static in vcpkg - dll

I'm using VS 2019 with vcpkg and boost 1.81
I've created a Dynamic Link Library Project (c++) and I've added the boost serialization library headers.
when I set vcpkg to use static libraries it builds fine , however if i set the "Use static Libraries" to No then I get the following compiler errors
Severity Code Description Project File Line Suppression State
Error C2491 'boost::archive::detail::archive_serializer_map<Archive>::insert': definition of dllimport function not allowed C:\dev\vcpkg\installed\x86-windows\include\boost\archive\impl\archive_serializer_map.ipp 41
Error C2491 'boost::archive::detail::archive_serializer_map<Archive>::erase': definition of dllimport function not allowed C:\dev\vcpkg\installed\x86-windows\include\boost\archive\impl\archive_serializer_map.ipp 49
Error C2491 'boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::basic_binary_iprimitive': definition of dllimport function not allowed C:\dev\vcpkg\installed\x86-windows\include\boost\archive\impl\basic_binary_iprimitive.ipp 146
Error C2491 'boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::~basic_binary_iprimitive': definition of dllimport function not allowed C:\dev\vcpkg\installed\x86-windows\include\boost\archive\impl\basic_binary_iprimitive.ipp 170
Error C2491 'boost::archive::detail::archive_serializer_map<Archive>::insert': definition of dllimport function not allowed C:\dev\vcpkg\installed\x86-windows\include\boost\archive\impl\archive_serializer_map.ipp 41
Error C2491 'boost::archive::detail::archive_serializer_map<Archive>::erase': definition of dllimport function not allowed C:\dev\vcpkg\installed\x86-windows\include\boost\archive\impl\archive_serializer_map.ipp 49
...[redacted]
what should i do to get it to build as a dynamic library?
UPDATE 2023/1/30
this issue happens only when adding the portable_binary_iarchive and portable_binary_iarchive files to the project. these files are not part of the standard boost library, however they are included in the examples.

you can create a CMake project, add the following commond in your CMakeLists.txt :
set(Boost_USE_STATIC_LIBS OFF)

Related

Building a rust library through CMake and using it as imported library target

I'm restucturing the CMake based build of a cross platform (macOS/Windows, Linux should be added soon) C++ project that has a third party rust library as dependendcy. Until now the rust lib dependency was supplied as precompiled library but I want to make its compilation part of my CMake build.
I got it working on macOS using the CMake makefile exporter by referencing the compiled library as a static imported library target and setting up a custom target with the command to build the rust library through cargo like this
add_library (rustlib STATIC IMPORTED)
add_custom_target (rustlib_cargo
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/Ext/rustlib/c-api
COMMAND cargo rustc --release -- --crate-type staticlib)
# Note: RUSTLIB_OUTPUT is set above refering to the absolute path of the produced platform specific library
set_target_properties (rustlib PROPERTIES IMPORTED_LOCATION ${RUSTLIB_OUTPUT})
add_dependencies (rustlib rustlib_cargo)
On macOS the cargo rustc command is invoked before the targets that link against my rustlib target are built and in case the rust library has been built previously this is detected by cargo and it just skips that compilation steph. But on Windows this fails with the built-in ninja exporter of Microsoft Visual Studio 2019 with an error like this:
ninja : error : '../../../Ext/rustlib/target/release/deps/rustlib.lib', needed by 'SomeTargetLinkingAgainstRustlib', missing and no known rule to make it
If I remove the line set_target_properties (rustlib PROPERTIES IMPORTED_LOCATION ${RUSTLIB_OUTPUT}) the build starts correctly, the rust build gets triggered, but as expected I end up with a linker error as the library to link against is not found. So is there any way to refer to a file that is not existent at configuration time but is guranteed to be created during compilation?

unknown type name '_declspec' on cmake

I am trying to generate a shared native library using cmake. the contents of this library are nothing more than native functions that will later be used in Unity. that is why the functions have the extension __declspec (dllexport) so that in the Unity section they can be called with dllImport.
My problem comes when I try to compile that library in cmake. I am using Ninja as compiled and cross-compiled with Android toolchain. The problem is that it tells me:
../InnovaeInterface/include/Camara.h:7:2: error: unknown type name '_declspec'
_declspec(dllexport) CameraUtils::Calibration* createCamara(void);
^
../InnovaeInterface/include/Camara.h:7:22: error: expected ';' after top level declarator
_declspec(dllexport) CameraUtils::Calibration* createCamara(void);
^
The truth is that I have no idea where to throw. I hope you help me.

Cmake - not creating the dynamic links

The project that I am compiling is not linking my shared object file to the main program. This can be confirmed by doing the ldd command on my executable and seeing it say libba.so => not found.
Inside my CMakeLists.txt file I have:
add_library(ba SHARED "/usr/local/include/libba.cpp" "/usr/local/include/libba.h")
target_link_libraries(ba (list of other libraries that link to ba))
set_target_properties(ba PROPERTIES LINK_INTERFACE_LIBRARIES "" LINK_FLAGS "${NO_UNDEFINED}")
add_executable(run "/usr/local/main/run.cpp")
target_link_libraries(run ba)
ldd reports what the run-time linker can find.
If you see libba.so in the output it means the binary is linked to the library.
The "not found" means the run-time linker can't find that library (i.e. you don't have it installed in a normal system location).
So you can either install your library to a system location, configure your run-time linker to know about your custom location, link your application statically, or use an rpath in your binary to have it give the run-time linker additional places to look for just itself.

MSVC 2008 - Unresolved External errors with LIB but only with DLL, not with EXE project

I have a DLL that I am trying to link with a libjpeg LIB using MSVC 2008 that is generating Unresolved External Symbol errors for the libjpeg functions. I also have a test project that links with the exact same libjpeg library file and links without error and runs fine too.
I have triple-checked my LIB path and dependent LIBS list settings and literally copy and pasted them from the EXE project to the DLL project. I still get the errors. I do have the libjpeg include headers surrounded by extern "C" so it is not a name mangling issue and the unresolved external warnings show the "missing" libjpeg functions as undecorated (just a leading underscore and the # sign parameter byte count suffix after each name).
What could make the linker with the DLL project be unable to find the functions properly when the test EXE project has no trouble at all? I'm using the pre-compiled 32-bit static multi-threaded debug library which I downloaded from ClanLib.
Thanks,
Robert
After a lot of experimentation I found the solution. It turns out the problem was due to a difference in the calling convention used by the DLL and the LIB projects (In MSVC 2008 this is set on the Project Properties, "Configuration Properties -> C/C++ -> Advanced" setting. The DLL project was set to __stdcall and the LIB was __cdecl. Recompiling LIBJPEG with __stdcall fixed the problem.

How to resolve Undefined Symbols error?

I'm getting this error
Undefined symbols:
".objc_class_name_MyClass", referenced from:
literal-pointer#__OBJC#__cls_refs#MyClass in infoViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
When referencing the static method below:
[MyClass ClickComment:self.navigationController];
MyClass is defined in a static library that I'm referencing in my app project. When I start typing "[MyClass "..., I get message hints. The app project knows MyClass exist and which methods are defined on it. Yet, I get the above error. Commenting out this call allows the project to build without error.
In the static library, I have a .h file that references all of the library's .h files. This way, the app project needs to reference only one .h file from the static library. The static library project also has an app. I use it to test the static library. I can do the above call fine. I usually see these types of errors when the static library has build a device/debug version and the app project has build simulator/debug. However, both builds are in sync.
I know there is a reference issue but I'm unsure how to resolve it. Any suggestions?
That means that the header files are found during compilation, but the linker is not aware of the static library. Make sure your static library is listed under "Targets -> YourMainTarget > Link Binary with Libraries" in the project view.
See http://developer.apple.com/tools/XCode/XCodeprojects.html