I am trying to read a jpg file with boost::gil. I started with the following code snippets but I got stuck already.
#include <iostream>
#include <vector>
#include <string>
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/jpeg_dynamic_io.hpp>
void ReadAnImage( std::string fname ){
std::vector<std::vector<float> > points;
boost::gil::rgb8_image_t img;
boost::gil::jpeg_read_image( fname , img );
}
int main( void ){
ReadAnImage( "pic.jpg" );
}
When I try to compile this code with
g++ -I ~/programs/cpp/boost/include/ -std=c++11 main.cpp -o main
I get the following error
MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x1a): undefined reference to jpeg_std_error' MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x3f): undefined reference to jpeg_CreateDecompress'
MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x61): undefined reference to jpeg_stdio_src' MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x76): undefined reference to jpeg_read_header'
/tmp/ccrGnMRc.o: In function boost::gil::detail::jpeg_reader::~jpeg_reader()': MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_readerD2Ev[_ZN5boost3gil6detail11jpeg_readerD5Ev]+0x18): undefined reference to jpeg_destroy_decompress'
/tmp/ccrGnMRc.o: In function void boost::gil::detail::jpeg_reader::apply<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::ve ctor3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t>, boost::mpl::range_c<int, 0, 3> > >*> > > >(boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned cha r, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t>, boost::mpl::range_c<int, 0, 3> > >*> > > const&)': MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0ELi 3EEEEEEEEEEEEEEEvRKT_[_ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0 ELi3EEEEEEEEEEEEEEEvRKT_]+0x2e): undefined reference to jpeg_start_decompress'
MainNew.cpp:(.text.ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0ELi
3EEEEEEEEEEEEEEEvRKT[ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0
ELi3EEEEEEEEEEEEEEEvRKT]+0x13e): undefined reference to jpeg_read_scanlines' MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0ELi 3EEEEEEEEEEEEEEEvRKT_[_ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0 ELi3EEEEEEEEEEEEEEEvRKT_]+0x1ad): undefined reference to jpeg_finish_decompress'
collect2: error: ld returned 1 exit status
I am not quite sure what I am doing wrong. Because according to the documentation
https://www.boost.org/doc/libs/1_74_0/libs/gil/doc/html/index.html
the jpeg_read_image should be included in boost::gil. Am I maybe missing another header file??
Any hints are much appreciated.
Related
I am pretty new to c and c++, so please try explain more specific what I should do. The program tries to read files from a directory using multithreads, and store the information in a map so that it can be used later.
I have been looking for similar posts. However, I am not able to figure out.
In https://github.com/kaldi-asr/kaldi/issues/938, it said that "If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro."
The solution for undefined reference to `pthread_cancel' (add "-pthread" flag does not work either.
My code is
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <random>
#include <unistd.h>
#include <cmath>
#include <stdlib.h>
#include <mutex>
#include <sys/wait.h>
#include <filesystem>
#include <string>
#include <pthread.h>
#define time_str(s) (s < 60 ? (to_string(s) + " second(s)") : (s < 3600 ? (to_string((s) / 60) + " minute(s)") : (to_string((s) / 3600) + " hour(s) and " + to_string(((s) % 3600) / 60) + " minute(s)")))
using namespace std;
namespace fs = std::filesystem;
struct MyGenom
{
vector<string> filepaths;
map<string, string> seq;
};
void check_rv(int rv) {
if (rv != 0) {
printf("Error: Value is %d\n", rv);
exit(1);
}
}
struct Reference_Genome {
static long unsigned int idx;
static map <string, string> seq;
static pthread_mutex_t mtxLock;
static vector <string> filepaths;
static void writing(string path) {
}
static void *distribution(void *var) {
}
Reference_Genome(string dir, unsigned int n_threads) {
}
};
int main(int argc, char const *argv[]) {
string dir = "./data/ex_seq";
unsigned int n_threads = 5;
Reference_Genome ref(dir, n_threads);
cout << "chr6: " << ref.seq["chr6"] << endl;
cout << "chr9: " << ref.seq["chr9"] << endl;
cout << "chr13: " << ref.seq["chr13"] << endl;
}
The gcc version is "Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)".
The error is
testSeq.cpp:97: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:98: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:99: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::writing(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
/testSeq.cpp:46: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:48: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::distribution(void*)':
testSeq.cpp:55: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:55: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:56: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:57: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:57: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:58: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:58: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:59: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::Reference_Genome(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int)':
testSeq.cpp:68: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:70: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:72: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:85: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:88: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
collect2: error: ld returned 1 exit status
When you declare static variables inside a class, you must also declare it exactly once outside of the class. In this case, you could put this in the bottom of your C++ file or in between the main() function and the class Reference_Genome definition:
long unsigned int Reference_Genome::idx;
map <string, string> Reference_Genome::seq;
pthread_mutex_t Reference_Genome::mtxLock;
vector <string> Reference_Genome::filepaths;
The idea is that you can put the class definition inside a header file, to be included in multiple different compilation units, but the static variables are only defined once, in one .cpp file of your choosing.
I have a function in C++ that uses data types like vector and map from STL. Here is some sample code:
mylib.cpp
#include "mylib.h"
#include<vector>
using namespace std;
int summation(int n) {
vector<int> numbers;
int sum = 0;
for(int i = 1; i <=n; i++)
numbers.push_back(i);
for(int j = 0; j < numbers.size(); j++)
sum += numbers[j];
return sum;
}
I created a header file as follows:
mylib.h
#ifdef _cplusplus
extern "C" {
#endif
extern int summation(int n);
#ifdef _cplusplus
};
#endif
The C++ file was compiled into the obejct code using the command
g++ -o mylib.o -c mylib.cpp
Then, I wrote a C program in order to use the function summation from it.
main.c
#include<stdio.h>
#include "mylib.h"
int main() {
int n;
scanf("%d", &n);
printf("%d", summation(n));
return 0;
}
Now, when I compile the C file above using gcc,
gcc main.c mylib.o
I get the following error
/tmp/ccAMN2ld.o: In function `main':
main.c:(.text+0x33): undefined reference to `summation'
mylib.o: In function `std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)':
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x26e): undefined reference to `__cxa_begin_catch'
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2d7): undefined reference to `__cxa_rethrow'
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2df): undefined reference to `__cxa_end_catch'
mylib.o: In function `std::vector<int, std::allocator<int> >::_M_check_len(unsigned long, char const*) const':
mylib.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[_ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc]+0x5b): undefined reference to `std::__throw_length_error(char const*)'
mylib.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x1c): undefined reference to `operator delete(void*)'
mylib.o: In function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)':
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
mylib.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
It is imperative that I use gcc to compile the C file. Is there a way to make it work?
I tried looking for solution and stumbled upon the links below,
How to call C++ function from C?
Elegantly call C++ from C
http://www.cplusplus.com/forum/general/8997/
but couldn't find a solution to my problem.
Compile the C file by itself with the C compiler:
gcc -c cfile.c
then link all the files with the C++ compiler:
g++ -o a.out main.o cfile.o mylib.o
Note that you must compile the file with the main function with the C++ compiler. Any other files can be compiled with either, though you need extern "C" declarations to be able to call C code from C++ or create C++ functions that can be called from C
I have build all the file in g++, am not getting linking error. can you build all the source file to gcc or g++.
I am trying to create a class with an std::function member:
# include<functional>
class Widget {
public:
std::function<int(double)> call_foo;
Widget(std::function<int(double)> call_func)
: call_foo(call_func)
{};
};
However, when I try to initialize the class my code fails:
const int f(double x){
if(x > 5.0){
return 22;
}
return 17;
}
int main()
{
std::function<int(double)> f1 = f;
Widget p(f1);
}
The full set of errors is below:
tmp/ccDoRHCh.o: In function `__static_initialization_and_destruction_0(int, int)':
widget.cpp:(.text+0x109): undefined reference to `std::ios_base::Init::Init()'
widget.cpp:(.text+0x118): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccDoRHCh.o:(.rodata._ZTIPFidE[_ZTIPFidE]+0x0): undefined reference to `vtable for __cxxabiv1::__pointer_type_info'
/tmp/ccDoRHCh.o:(.rodata._ZTIFidE[_ZTIFidE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info'
/tmp/ccDoRHCh.o:(.eh_frame+0xab): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
I am compiling with C++ 11 enabled.
Chances are either you forgot
#include <functional>
or you'r not compiling in C++11 mode.
I've searched existing questions, but I can't seem to solve this.
I have a blur_mask.cc file with:
#include <iostream>
#include <ctime>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <vector_types.h>
#include <cuda.h>
#include <cuda_runtime.h>
extern "C" void gpuBlurMask(unsigned char* srcData, int srcStep, uchar3* dst, int dstStep, int width, int height, float *mask, int maskStep, int maskWidth, int maskHeight, int blockSize=16);
using namespace std;
using namespace cv;
using namespace cv::gpu;
void blurMask(Mat& src, Mat& dst, Mat& mask) {
GpuMat gpuSrc, gpuDst, gpuMask;
gpuSrc.upload(src);
gpuDst.upload(dst);
gpuMask.upload(mask);
gpuBlurMask(gpuSrc.data, gpuSrc.step, gpuDst.ptr<uchar3>(), gpuDst.step, gpuSrc.cols, gpuSrc.rows, gpuMask.ptr<float>(), gpuMask.step, gpuMask.cols, gpuMask.rows);
gpuDst.download(dst);
}
...
And a gpu_blur.cu file that contains
extern "C"
void gpuBlurMask(unsigned char* srcData, int srcStep, uchar3* dst, int dstStep, int width, int height, float *mask, int maskStep, int maskWidth, int maskHeight, int blockSize=16) {
...
When I do
nvcc -c -o gpu_blur gpu_blur.cu
I get no errors, but when compiling
g++ -o blur_mask blur_mask.cc gpu_blur -I /usr/local/cuda/include/ -lopencv_core -lopencv_highgui -lopencv_gpu
I get the following errors:
g++ -o blur_mask blur_mask.cc gpu_blur -I /usr/local/cuda/include/ -lopencv_core -lopencv_highgui -lopencv_gpu -lcuda
gpu_blur: In function `gpuBlurMask':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x59): undefined reference to `cudaMallocArray'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0xa0): undefined reference to `cudaMemcpy2DToArray'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x139): undefined reference to `cudaConfigureCall'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x16f): undefined reference to `cudaDeviceSynchronize'
gpu_blur: In function `__cudaUnregisterBinaryUtil()':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x184): undefined reference to `__cudaUnregisterFatBinary'
gpu_blur: In function `__device_stub__Z14blurMaskKernelP6uchar3iiiPfiii(uchar3*, int, int, int, float*, int, int, int)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x1b9): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x1dc): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x1ff): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x222): undefined reference to `cudaSetupArgument'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x245): undefined reference to `cudaSetupArgument'
gpu_blur:tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x264): more undefined references to `cudaSetupArgument' follow
gpu_blur: In function `__nv_cudaEntityRegisterCallback(void**)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x381): undefined reference to `__cudaRegisterFunction'
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x3af): undefined reference to `__cudaRegisterTexture'
gpu_blur: In function `__sti____cudaRegisterAll_43_tmpxft_00000905_00000000_6_gpu_blur_cpp1_ii_srcTex()':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text+0x3c3): undefined reference to `__cudaRegisterFatBinary'
gpu_blur: In function `cudaChannelFormatDesc cudaCreateChannelDesc<unsigned char>()':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text._Z21cudaCreateChannelDescIhE21cudaChannelFormatDescv[cudaChannelFormatDesc cudaCreateChannelDesc<unsigned char>()]+0x34): undefined reference to `cudaCreateChannelDesc'
gpu_blur: In function `cudaError cudaBindTextureToArray<unsigned char, 2, (cudaTextureReadMode)0>(texture<unsigned char, 2, (cudaTextureReadMode)0> const&, cudaArray const*, cudaChannelFormatDesc const&)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text._Z22cudaBindTextureToArrayIhLi2EL19cudaTextureReadMode0EE9cudaErrorRK7textureIT_XT0_EXT1_EEPK9cudaArrayRK21cudaChannelFormatDesc[cudaError cudaBindTextureToArray<unsigned char, 2, (cudaTextureReadMode)0>(texture<unsigned char, 2, (cudaTextureReadMode)0> const&, cudaArray const*, cudaChannelFormatDesc const&)]+0x27): undefined reference to `cudaBindTextureToArray'
gpu_blur: In function `cudaError cudaLaunch<char>(char*)':
tmpxft_00000905_00000000-3_gpu_blur.cudafe1.cpp:(.text._Z10cudaLaunchIcE9cudaErrorPT_[cudaError cudaLaunch<char>(char*)]+0x14): undefined reference to `cudaLaunch'
collect2: ld returned 1 exit status
Thank you.
You need to link to cudart library (-L/usr/local/cuda/lib64 -lcudart)
I am just trying to make a vector, but it gives me a huge error and I am following a working example from my other project. The code:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct organism {
bool One;
bool Two;
};
std::vector<organism> organisms;
int main() {
printf("Content-type: text/html\n\n");
printf("TEST");
printf(getenv("QUERY_STRING"));
return 0;
}
The error:
> "make"
C:/MinGW/bin/gcc.exe -o build/e2.exe source/main.cpp
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.text$_ZN9__gnu_cxx13new_allocatorI8organismE10deallocateEPS1_j[__gnu_cxx::new_allocator<organism>::deallocate(organism*, unsigned int)]+0xd): undefined reference to `operator delete(void*)'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt12_Vector_baseI8organismSaIS0_EED2Ev+0x13): undefined reference to `__gxx_personality_v0'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt6vectorI8organismSaIS0_EED1Ev+0x13): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
"make": *** [build] Error 1
> Process Exit Code: 2
> Time Taken: 00:01
I can compile it if I comment out std::vector<organism> organisms; but I have no clue what's wrong with that line. It's exactly the same in my other project, which compiles fine.
You need to compile with g++.exe instead of gcc.exe so that it will know it needs to link with the C++ library.