undefined reference when compiling c++ project with g++ - c++

I am trying to create a simple c++ application that utilizes the libheif library.
My setup is Visual Studio Code and msys2, I followed the recommended setup guide for c++ and vscode
I have started by including the file libheif into one of the paths obtained using the command cpp -v,
I then tried to compile the basic example found on the libheif github page
#include <iostream>
#include <string>
extern "C" {
#include <libheif/heif.h>
}
int main() {
const char *input_filename = "test.heic";
heif_context* ctx = heif_context_alloc();
heif_context_read_from_file(ctx, input_filename, nullptr);
// get a handle to the primary image
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);
// decode the image and convert colorspace to RGB, saved as 24bit interleaved
heif_image* img;
heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGB, nullptr);
int stride;
const uint8_t* data = heif_image_get_plane_readonly(img, heif_channel_interleaved, &stride);
}
I compile it and get the following errors
C:\msys64\mingw64\bin\g++.exe -g D:\Projects\ImageConverter\heic2jpg.cpp -o D:\Projects\ImageConverter\heic2jpg.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\wetha\AppData\Local\Temp\ccz86gv8.o:D:/Projects/ImageConverter/heic2jpg.cpp:10: undefined reference to `heif_context_alloc'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\wetha\AppData\Local\Temp\ccz86gv8.o: in function `main':
D:/Projects/ImageConverter/heic2jpg.cpp:11: undefined reference to `heif_context_read_from_file'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Projects/ImageConverter/heic2jpg.cpp:15: undefined reference to `heif_context_get_primary_image_handle'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Projects/ImageConverter/heic2jpg.cpp:19: undefined reference to `heif_decode_image'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Projects/ImageConverter/heic2jpg.cpp:22: undefined reference to `heif_image_get_plane_readonly'
collect2.exe: error: ld returned 1 exit status
VCPKG
g++
for g++ i ran the command
g++ main.cpp -LD:/vcpkg -llibheif
cmake
i installed cmake and created a CMakeLists.txt containing
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(libheif)
add_executable(main heic2jpg.cpp)
target_link_libraries(main PRIVATE libheif::heif)
then ran the command
cmake .. "-DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake"
and got errors
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -llibheif
and got errors
Could not find a package configuration file provided by "libheif" with any
of the following names:
libheifConfig.cmake
libheif-config.cmake
i have tried both of these with different varients of package names such has libheif_x86-windows.

Related

Undefined reference to glewinit, glfwExperimental and many more [duplicate]

This question already has an answer here:
glfw Errors with glfwWindowHint
(1 answer)
Closed 2 years ago.
I am very new to c++ and I am trying to use opengl with glfw. I am using ubuntu 18 on wsl. I have GL and GLFW on my include directory and I have included it in my code.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
using namespace glm;
int main(){
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Hi\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
}
I am using g++ to compile the program. When I use g++ -c main.cpp it compiles but throws a binary error when I try to run the program and when I use g++ main.cpp -IGL -IGLFW -Iglm I get that error. Am I missing something?
Error:
/usr/bin/ld: /tmp/cco9YYTw.o: in function `main':
main.cpp:(.text+0x17): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x26): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x35): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x44): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x53): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x74): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0xa4): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0xb7): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text+0xbd): undefined reference to `glewExperimental'
/usr/bin/ld: main.cpp:(.text+0xc3): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status
To build your example you should install the following dependencies on your Ubuntu with apt-get
libglm-dev, libglfw-dev, libglew-dev, cmake
I've used CMake to build your file
Put the following CMakeLists.txt file in your folder where .cpp file located (I copied your code into a file named glfw_sample.cpp)
cmake_minimum_required(VERSION 3.10)
project(glfw_sample)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
add_executable(${PROJECT_NAME} glfw_sample.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE GLEW::GLEW ${GLFW_LIBRARIES})
Then execute in a shell
cmake -S. -Bbuild
cmake --build build
If no errors occurred on both steps you'll find glfw_sample executable in the build folder.
I use the following links to build your code:
https://www.glfw.org/docs/3.0/build.html
https://cmake.org/cmake/help/latest/module/FindGLEW.html
A very quick google search might help: glfw-errors-with-glfwwindowhint
Link to -lglfw3 not -lglfw - if this is correct, please vote up the linked-to-answer and not this one
To cover the location of the library, from compile-glfw-on-ubuntu-and-fix-libglfw-so-cannot-open-error
This is because the GLFW installer puts libglfw.so in /usr/local/lib
instead of /usr/lib like Ubuntu expects. But /usr/local/lib is a fine
place to put shared libraries for most of the Unix world, so let’s
just tell Ubuntu to look there when linking our program. You’ll want
to edit /etc/ld.so.conf and add the line “/usr/local/lib” to the file,
then save. Afterwards you should run ldconfig as root. Something like
this:
sudo vim /etc/ld.so.conf
# Add the path and save - then try to build again

g++ building c++ program with boost dependencies

I want to compile and run a simple c++ websocket application with g++ on windows.
Boost was installed like this:
./bootstrap.bat mingw
./b2.exe install --prefix=C:/boostLibs toolset=gcc
My c++ includes look like this:
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
This websocket sample was provided by https://github.com/zaphoyd/websocketpp
In order to build the project I issue this command:
g++ -Wno-deprecated -I ./cppServer/libs/ -I C:\boostLibs\include\boost-1_55 -L C:\boostLibs\lib -g ./cppServer/server.cpp -lboost_system
Which leeds me to this error message:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lboost_system
collect2.exe: error: ld returned 1 exit status
If I try to build without -lboost_system, I get a very long exception, starting with:
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()'
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:223: undefined reference toboost::system::generic_category()'
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:224: undefined reference to boost::system::system_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost6system10error_codeC1Ev':
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:323: undefined reference to boost::system::system_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost6system4errc20make_error_conditionENS1_6errc_tE':
C:/boostLibs/include/boost-1_55/boost/system/error_code.hpp:488: undefined reference to boost::system::generic_category()'
C:\Users\JOHANN~1.HAS\AppData\Local\Temp\ccpKMWTH.o: In functionZN5boost16thread_exceptionC2EiPKc'
So what am I missing? I can't figure it out right now.
Link to boost_system as -lboost_system-mgw63-mt-1_55, because this is what those files are called. See boost library naming for more details.
When you build boost you may like to specify --layout=system to b2.exe so that your files do not have that -mgw63-mt-1_55 in the filename and then just use -lboost_system when linking against it.

Boost "undefined reference" errors even with -lboost_thread

I'm getting some strange compiler/linker errors when trying to use boost::shared_mutex. I'm using boost v1.61 on a VM running 32-bit rhel 6.2.
Code that causes error:
hpp file:
#include <boost/thread/shared_mutex.hpp>
class SharedData
{
public:
SharedData();
~SharedData();
void packMessage(std::shared_ptr<Message> s);
private:
// mutex that allows multiple read, single write protection
boost::shared_mutex m_sharedMutex;
};
cpp file:
void SharedData::packMessage(std::shared_ptr<Message> s)
{
// get shared read access
boost::shared_lock<boost::shared_mutex> lock(m_sharedMutex); // <- this line causes the errors
// read stuff here
}
make output:
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:195: undefined reference to `boost::detail::get_current_thread_data()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:81: undefined reference to `boost::this_thread::interruption_point()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::shared_mutex::lock_shared()':
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/tester] Error 1
Everywhere I've searched has said that these errors mean I need to link the boost_thread library, which I've done in my cmake file (boost_system is included for other code in this same project):
target_link_libraries(${BINARY_NAME} boost_thread boost_system)
But the errors persist.
Originally I was using the boost 1.41 libraries that were already installed on my machine. When I hit these errors I ran yum remove boost-devel and then manually installed boost 1.61 to see if that would correct the errors. Is there something extra I needed to add to the ./bootstrap.sh or ./b2 install commands? I can see libboost_thread.a in /usr/local/lib, so I assumed that meant the thread library was built correctly.
Any thoughts on what is causing these errors? Thanks!
Edit:
Cmake's log didn't have anything useful I could see, but make VERBOSE=1 gives the following output:
cd /home/craig/dev/myProject/build/test && /usr/bin/cmake -E cmake_link_script CMakeFiles/tester.dir/link.txt --verbose=1
/opt/rh/devtoolset-2/root/usr/bin/c++ -std=c++11 -ggdb -Wall -Werror -fprofile-arcs -ftest-coverage -fPIC -O0 -pedantic -Wl,--export-dynamic CMakeFiles/tester.dir/utilities/googletest/googletest/src/gtest-all.cc.o CMakeFiles/tester.dir/tester.cpp.o CMakeFiles/tester.dir/SharedData/testSharedData.cpp.o CMakeFiles/tester.dir/Common/testFifo.cpp.o CMakeFiles/tester.dir/Common/testCsu.cpp.o CMakeFiles/tester.dir/Messages/testMessage.cpp.o CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o CMakeFiles/tester.dir/__/src/Common/Fifo.cpp.o CMakeFiles/tester.dir/__/src/Common/Csu.cpp.o -o ../bin/tester -rdynamic -lboost_system -lpthread
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:195: undefined reference to `boost::detail::get_current_thread_data()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:81: undefined reference to `boost::this_thread::interruption_point()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::shared_mutex::lock_shared()':
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
Turns out my question is just like everyone else's.
#jww suggested I show the actual compile and link command invocation, not CMake's output, which I've added to my question. As you can see, the command is linking boost_system and pthread, but not boost_thread.
Digging into my project I realized that I had added boost_thread to my release build, but not into my unit test build, which is what I was trying to compile. Adding boost_thread to the unit test's CMakeLists.txt removed the error immediately.

Bullet basic project error: undefined reference to `btTypedConstraint::serialize(void*, btSerializer*) const'

I have been trying to link bullet to an existing project. I was able to compile and install the source and the example programs, but I can't compile a basic empty file.
here is the test.cpp code:
#include "btBulletDynamicsCommon.h"
#include <stdio.h>
int main()
{
return 0;
}
and the error:
g++ -std=c++11 -o run test.cpp -I /usr/local/include/bullet/ /usr/local/lib/libBullet3Dynamics.a /usr/local/lib/libBulletCollision.a /usr/local/lib/libLinearMath.a
/tmp/ccvDsyTQ.o:(.rodata._ZTV17btTypedConstraint[_ZTV17btTypedConstraint]+0x60): undefined reference to `btTypedConstraint::serialize(void*, btSerializer*) const'
collect2: error: ld returned 1 exit status
Compilation failed.
The btBulletDynamicsCommon.h header includes the 2.x part of the library, which requires libBulletDynamics.a. Version 3 (libBullet3Dynamics.a) is incomplete and not recommended for use.

HDF5 - C++ - open a file to read the contents failed

I tried writing a really short script just to open an hdf5 file but it does not work.
#include <iostream>
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "testfile.h5" );
int main (void)
{
H5File openFile( FILE_NAME, H5F_ACC_RDONLY );
}
I'm pretty sure that I included the hdf5 library and the path to the includes.
But nevertheless I get the error message from the linker:
Invoking: GCC C++ Linker
g++ -L/usr/local/pub/lib64 -L/usr/local/pub/lib -L/lib64 -L/usr/lib64 -o "HDF5_CPP" ./openfile.o
./openfile.o: In function `main':
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5check_version'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::FileAccPropList::DEFAULT'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::FileCreatPropList::DEFAULT'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::H5File::H5File(std::string const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::H5File::~H5File()'
collect2: error: ld returned 1 exit status
make: *** [HDF5_CPP] Error 1
can anyone help?
Thank You!
For those using CMake, here is an example:
(the undefined reference problem is solved in the last line)
find_package(HDF5 COMPONENTS C CXX HL REQUIRED)
link_directories( ${HDF5_LIBRARY_DIRS} )
include_directories( ${HDF5_INCLUDE_DIRS} )
add_executable( convert_to_hdf5 src/convert_to_hdf5.cpp )
target_link_libraries( convert_to_hdf5 ${HDF5_CXX_LIBRARIES} )
I'd just like to leave a note to everyone else who reaches this place in the future, struggling with the same problem:
If you're choosing to use g++ with flags to compile your code instead of the hdf5-provided h5c++ script, make sure the flags you use are from h5c++ -show instead of h5cc -show, as the latter is for the straight C version.