Assimp - Windows - CMakeList - Failing to import - c++

I know this question has been asked before (lot of times).
How to include Assimp with CMakeLists?
CMakeLists Including OpenGL, glew, glfw, glm and assimp
But I still cannot get it to work (normally).
Question: How to install Assimp and use with find_package in cmakelist.txt
What I tried:
Made a build with the cmake GUI and then with Visual studio build the
sln file
Installed it with vcpck and added the path to
cmakelist.txt
add_library(assimp "lib/assimp/assimp.dll")
But all of the methods give errors like: cannot not find assimp, or missing Assimp:Importer
include_directories("${LIB_DIR}/assimp/include")
# target_link_libraries(${PROJECT_NAME} assimp)
find_package(assimp REQUIRED)
if (assimp_FOUND)
set(ASSIMP_LIBRARY "assimp")
add_library(${ASSIMP_LIBRARY} SHARED IMPORTED)
set_target_properties(${ASSIMP_LIBRARY} PROPERTIES IMPORTED_LOCATION "${LIB_DIR}/assimp/libassimp.so")
target_link_libraries(${PROJECT_NAME} ${ASSIMP_LIBRARIES})
endif(assimp_FOUND)
This seems to build, but then I get errors like unresolved external symbol "public: __cdecl Assimp::Importer::Importer(void)" (??0Importer#Assimp##QEAA#XZ) referenced in function "private: void __cdecl
So there is something I am missing entirely and I dont understand it apparently.
Is there something simple I'm doing wrong? Thanks!

Related

How can I solve error LNK2019 with GLFW in librealsense example code project "capture" for c++

I've looked and tried the solutions provided in the many other posts regarding my issue but none of them worked for me. So I've been working on a project that requires the use of librealsense, a library which allows realsense camera users to do many things with their cameras. In order to understand better the potential of this library, I tried building some of their examples with a CMakeLists.txt on Visual Studio 2022 (I can't have a Linux OS). I successfully built and executed the first example "Hello-Realsense" so I think it's possible to build with a CMakeList on Visual Studio. However, the second project ( https://github.com/IntelRealSense/librealsense/tree/master/examples/capture ) gives me 42 unresolved externals, here is a few :
[...]tuto_librealsense\2_capture\out\build\x64-Debug\LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
[...]tuto_librealsense\2_capture\out\build\x64-Debug\rs-capture.cpp.obj : error LNK2019: unresolved external symbol __imp_glBegin referenced in function "void __cdecl draw_pointcloud(float,float,struct glfw_state &,class rs2::points &)" (?draw_pointcloud##YAXMMAEAUglfw_state##AEAVpoints#rs2###Z)
[...]tuto_librealsense\2_capture\out\build\x64-Debug\rs-capture.cpp.obj : error LNK2019: unresolved external symbol __imp_glBindTexture referenced in function "void __cdecl draw_pointcloud(float,float,struct glfw_state &,class rs2::points &)" (?draw_pointcloud##YAXMMAEAUglfw_state##AEAVpoints#rs2###Z)
There is also that warning which says there might be a conflict between my OS libs and the libs I try to link as you can see.
Here is an overview of my project organization :
my CMakeLists.txt, example.hpp and rs-capture.cpp
The example.hpp file is a header which must be included in the project according to the example creator. It is this file which forces me to use the GLFW library which I guess I must be linking incorrectly, although I followed the same logic which allowed me to build correctly the Hello-Realsense project which is placing the .lib files in the DEPENDENCIES macro and the header files of my libs in the PATH_TO_HEADERS macro. Also I took the glfw3.lib file from the lib-vc2022 folder since my Visual Studio version is 2022 but I admit this is a little arbitrary.
cmake_minimum_required(VERSION 3.1.0)
project(RealsenseExamplesCapture)
add_executable(rs-capture rs-capture.cpp example.hpp)
set_property(TARGET rs-capture PROPERTY CXX_STANDARD 11)
set(DEPENDENCIES "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib" "C:/Program Files/GL/GLFWx64/lib-vc2022/glfw3.lib")
set(PATH_TO_HEADERS "C:/Program Files (x86)/Intel RealSense SDK 2.0/include/" "C:/Program Files/GL/GLFWx64/include/GLFW" "C:/Program Files (x86)/Intel RealSense SDK 2.0/samples")
target_link_libraries(rs-capture ${DEPENDENCIES})
INCLUDE_DIRECTORIES(${PATH_TO_HEADERS})
install(TARGETS rs-capture RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Also, I put the glfw3.dll file in the C:\Windows\System32 directory as I saw on a WikiHow tutorial.
Someone helped me IRL so I'll share my newly acquired knowledge. As you probably know, when there is an "unresolved external symbol" error it means the compiler cannot find where is the symbol. Below are the steps to fix this :
Find the name of the function or symbol which isn't found.
Example :
[...]\tuto_librealsense\2_capture\out\build\x64-Debug\rs-capture.cpp.obj: error LNK2019: unresolved external symbol gluPerspective referenced in function "void __cdecl draw_pointcloud(float,float,struct glfw_state &,class rs2::points &)" (?draw_pointcloud##YAXMMAEAUglfw_state##AEAVpoints#rs2###Z)
Here you can see the symbol "draw_pointcloud" isn't resolved which leads us to the next step : research.
So you first search for it in your solution. In my case I found it in example.hpp and it was a function definition :
void draw_pointcloud(float width, float height, glfw_state& app_state, rs2::points& points)
{
if (!points)
return;
// OpenGL commands that prep screen for the pointcloud
glLoadIdentity();
glPushAttrib(GL_ALL_ATTRIB_BITS);
Now you can see they are some other symbols which are not standard C++ syntax such as glLoadIdentity so you search glLoadIdentity on the internet : in my case the first result I got was the Microsoft website because it's a native Windows library. On this page I found that this function belongs to the Opengl32 library and the corresponding file is Opengl32.lib which leads us to the next step :
Locate the .lib file on your PC or download it. In this case it's a native library so you go and search it in C:\Program Files\ or C:\Program Files (x86)\. Notice that the 32 at the end the filename indicates you to search it on the the directory ending by (x86) since it means it's a 32 bit library.
Give the location of the .lib file to your CMakeLists.txt ( or in the property pages of your C++ project).
Example : the two lines I modified in my CMakeLists.txt :
set(DEPENDENCIES "C:/Program Files (x86)/Intel RealSense SDK 2.0/lib/x64/realsense2.lib" "C:/Program Files/GL/GLFWx64/lib-vc2022/glfw3.lib" "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/um/x64/OpenGL32.Lib" "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/um/x64/GlU32.Lib")
set(PATH_TO_HEADERS "C:/Program Files (x86)/Intel RealSense SDK 2.0/include/" "C:/Program Files/GL/GLFWx64/include/GLFW" "C:/Program Files (x86)/Intel RealSense SDK 2.0/samples")

Boost::python does not build in Debug mode

I am working on a system that needs to embed python in a C++ program on Windows. I installed boost with the boost python packages and everything seems to be correct. When I run CMake it finds the debug and the release libraries from Boost python (the mt and mt-gd ones) and links them correctly with the program via TARGET_LINK_LIBRARIES. However when I build the project in Debug mode the compiler refuses to build, but when I build in Release mode everything is builds fine.
Here is the way I link the library:
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development REQUIRED)
FIND_PACKAGE(Boost COMPONENTS python REQUIRED)
add_executable(ExampleProject "Example.cpp")
TARGET_LINK_LIBRARIES(ExampleProject Boost::python)
TARGET_INCLUDE_DIRECTORIES(ExampleProject PUBLIC ${Boost_INCLUDE_DIR})
I have checked that the correct boost library is linked and I have used the Boost_PYTHON_LIBRARY_DEBUG, to which the compiler says it cannot open the library:
fatal error LNK1104: cannot open file 'boost_python310-vc143-mt-gd-x32-1_79.lib'
I did notice that it does not say libboost here, but when checking the linker it does say libboost, I guess the error output is just different.
When I use Boost::python in CMake it does link but it throws unresolved external symbols like this one:
error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl boost::python::throw_error_already_set(void)" (__imp_?throw_error_already_set#python#boost##YAXXZ) referenced in function "struct _object * __cdecl boost::python::expect_non_null<struct _object>(struct _object *)" (??$expect_non_null#U_object###python#boost##YAPAU_object##PAU2##Z)
I have also tried to set the Boost_USE_DEBUG_LIBS and Boost_USE_RELEASE_LIBS, but that did not change anything.
Furthermore the compiler does give the warning that release and debug are mixed:
warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
Which does indicate that they have been mixed up, but when I check the linker it uses the libboost_python310-vc143-mt-gd-x32-1_79.lib which is the Debug version of the python library.
Did I miss anything? Did I do something wrong? I do not understand why this happens and I am still fairly new to CMake and Boost, but I feel like I did all that I could to make it work.

Using wxWidgets with CMAKE and visual studio

Every time I try to build my project with CMAKE having the wxWidgets as library I get a lot of linker errors LNK I even get two errors for just compiling my main.cpp that only includes this :
#include "wx/wx.h"
int main(){
return 0;
}
I just downloaded the source code of wxwidgets, put it in a lib folder and linked it with cmake
This is my CMakeLists.txt :
cmake_minimum_required(VERSION 3.10)
# set the project name
project(wxtest VERSION 1.0)
add_subdirectory(libs/wxWidgets)
add_executable(wx_test WIN32 src/main.cpp)
target_link_libraries(wx_test wx::net wx::core wx::base)
Here are the errors:
Error LNK1120 1 unresolved externals
Error LNK2019 unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
Is there anything wrong with linking the wxWidgets lib to my project ?
wxWidgets programs are compiled as GUI Win32 applications and so use WinMain() entry point by default, which you don't define.
The usual way to define it is by using wxIMPLEMENT_APP() macro, but you may also do it manually if you really need to -- but you must define it, one way or the other.

CMake project cannot link with Boost.Test statically or dynamically

I am trying to setup a simple C++ project with CMake and Boost libraries (Boost.Test in this case). I am following this guide except trying to link statically: https://www.jetbrains.com/help/clion/boost-test-support.html. I have installed Boost using this guide: https://www.boost.org/doc/libs/1_73_0/more/getting_started/windows.html and successfully followed step 5 to generate the library binaries.
Using the below CMake configuration, I get this error when trying to build with cmake --build .:
test1.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl boost::unit_test::unit_test_log_t::test_start(unsigned long)" (?test_start#unit_test_log_t#unit_test#boost##UEAAXK#Z)
tests2.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl boost::unit_test::unit_test_log_t::test_start(unsigned long)" (?test_start#unit_test_log_t#unit_test#boost##UEAAXK#Z)
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(cmake_boost_test)
set(CMAKE_CXX_STANDARD 14)
add_executable(cmake_boost_test main.cpp)
add_subdirectory(Boost_tests)
Boost_tests/CMakeLists.txt
set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(Boost_Tests_run test1.cpp tests2.cpp)
target_link_libraries(Boost_Tests_run ${Boost_LIBRARIES})
Boost_tests/test1.cpp
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
Boost_tests/tests2.cpp
#include <boost/test/unit_test.hpp>
There are basically 3 steps:
building boost and the relevant libraries and their variant (see here for Boost.Test)
configure the project with CMake (an example here)
build the project
It seems like you have all the steps, but what makes things difficult is that those 3 steps are related and should be done in a coherent manner:
Step 1+2: if you are building specific variants of Boost (or Boost.Test) in step 1, you have to instruct CMake with that variant
Step 3: when you build your project, your code program should in some cases be build with defines that instruct what is the variant being built. For instance if you want to link with the shared library version of Boost.Test, you have to define BOOST_TEST_DYN_LINK (in the code before any include of Boost.Test or with target_compile_definitions(test_executable PRIVATE "BOOST_TEST_DYN_LINK=1"))
In the case of Boost.Test, there is another gotcha: on Windows there is the autolink feature that will instruct the linker to link automatically with one version of a .lib: this is something I usually disable by passing a define BOOST_ALL_NO_LIB.
If you are able to make CMake detect for Boost.Test, I would not bother at the beginning with all those linking issues and use the header variant for which no linking issue is involved.
Coming back to what you have currently:
the error Could NOT find Boost (missing: unit_test_framework) (found version 1.60.0") is related to step 1+2: CMake is unable to find the libraries with the configuration you are indicating. Sometimes you need to give more variables to CMake such that it finds the right libraries, but my guess is that you just did not build the shared variant (see here). For instance I use Boost_COMPILER and Boost_ARCHITECTURE sometimes.
Boost_USE_STATIC_LIBS is an instruction for CMake only: it will not enforce the compilation of your with the right defines.
If you post the error that you get with CMake by passing -DBoost_DEBUG=ON, we will be able to support you with more precision.

OpenGL in Visual studio - Issues with GLEW

I'm currently following the openglbook.com tutorials (set up) (Tutorial) and have run into an issue fairly early on. I'm getting the following two errors:
1>main.obj : error LNK2019: unresolved external symbol __imp__glewGetErrorString#4 referenced in function _Initialize
1>main.obj : error LNK2019: unresolved external symbol __imp__glewInit#0 referenced in function _Initialize
I have downloaded and compiled freeglut 2.8.0 as well as the glew 1.9.0 binaries. I have copied the libs and includes to C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\Lib and Include respectively.
The Lib and Include paths have also been added to my project properties under Additional Library/Include directories.
I have also defined glew32.lib and freeglut.lib in my linker->Input->Additional dependencies.
I have included GL/glew.h and GL/freeglut.h at the top of my main file.
What am I missing? Every other thread I've found has been solved by adding the directories to the project properties. Does anyone have any ideas?
This means that you try to use GLEW as a DLL (because your application looks for a name that begins with __imp, like "import" ), but you didn't built GLEW as a DLL (because otherwise it would work).
3 possible options :
Rebuild GLEW with the GLEW_BUILD preprocessor definition (Project->Properties->C++->Preprocessor->Additional definitions). Then rebuild your application.
Don't build GLEW at all. Simply put glew.c in your application's project. This is the easiest way.
(my favourite) Define GLEW_STATIC in your application's preprocessor definitions, and rebuild.