I have an old source which I need to integrate in my project in Eclipse (in Ubuntu 12.04).
The old source code works perfectly on its own. But the only problem with it is - I have just one .cpp file (which contains the main) and all others are .h files which contains the definitions and declarations. I fixed this by creating a source file for each header file and copying the function definitions in these files and left the declarations in the header files.
I added these files using eclipse so those files actually are getting compiled.
Note that, I have added this source code into "src/segment". Here is my directory listing. These files also show up in the eclipse project. My main function is in "HelloOpenCV2.cpp"
[eeuser#roadrunner src]$ ls
CameraPoseFromFundamentalMatrix.d InterestPoints.o
CameraPoseFromFundamentalMatrix.o LinesAndComponents.d
GLViz.d LinesAndComponents.o
GLViz.o MorphologicalManip.d
GraphImageSegmentation.d MorphologicalManip.o
GraphImageSegmentation.o MultiviewColorConsistency.d
HelloOpenCV2.d MultiviewColorConsistency.o
HelloOpenCV2.o segment
HomographyWarp.d StereoCamDepth.d
HomographyWarp.o StereoCamDepth.o
ImageManipulations.d subdir.mk
ImageManipulations.o VideoProc.d
InterestPoints.d VideoProc.o
[eeuser#roadrunner src]$ ls segment/
convolve.d filter.d imconv.d segment-graph.d subdir.mk
convolve.o filter.o imconv.o segment-graph.o
disjoint-set.d image.d imutil.d segment-image.d
disjoint-set.o image.o imutil.o segment-image.o
Here is my compilation log as generated by eclipse.
18:54:21 **** Build of configuration Debug for project HelloOpenCV2 ****
make all
Building file: ../src/segment/convolve.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/convolve.d" -MT"src/segment/convolve.d" -o "src/segment/convolve.o" "../src/segment/convolve.cpp"
Finished building: ../src/segment/convolve.cpp
Building file: ../src/segment/disjoint-set.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/disjoint-set.d" -MT"src/segment/disjoint-set.d" -o "src/segment/disjoint-set.o" "../src/segment/disjoint-set.cpp"
Finished building: ../src/segment/disjoint-set.cpp
Building file: ../src/segment/filter.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/filter.d" -MT"src/segment/filter.d" -o "src/segment/filter.o" "../src/segment/filter.cpp"
Finished building: ../src/segment/filter.cpp
Building file: ../src/segment/image.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/image.d" -MT"src/segment/image.d" -o "src/segment/image.o" "../src/segment/image.cpp"
Finished building: ../src/segment/image.cpp
Building file: ../src/segment/imconv.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/imconv.d" -MT"src/segment/imconv.d" -o "src/segment/imconv.o" "../src/segment/imconv.cpp"
Finished building: ../src/segment/imconv.cpp
Building file: ../src/segment/imutil.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/imutil.d" -MT"src/segment/imutil.d" -o "src/segment/imutil.o" "../src/segment/imutil.cpp"
Finished building: ../src/segment/imutil.cpp
Building file: ../src/segment/segment-graph.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/segment-graph.d" -MT"src/segment/segment-graph.d" -o "src/segment/segment-graph.o" "../src/segment/segment-graph.cpp"
Finished building: ../src/segment/segment-graph.cpp
Building file: ../src/segment/segment-image.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/segment/segment-image.d" -MT"src/segment/segment-image.d" -o "src/segment/segment-image.o" "../src/segment/segment-image.cpp"
Finished building: ../src/segment/segment-image.cpp
Building file: ../src/CameraPoseFromFundamentalMatrix.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/CameraPoseFromFundamentalMatrix.d" -MT"src/CameraPoseFromFundamentalMatrix.d" -o "src/CameraPoseFromFundamentalMatrix.o" "../src/CameraPoseFromFundamentalMatrix.cpp"
Finished building: ../src/CameraPoseFromFundamentalMatrix.cpp
Building file: ../src/GLViz.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/GLViz.d" -MT"src/GLViz.d" -o "src/GLViz.o" "../src/GLViz.cpp"
Finished building: ../src/GLViz.cpp
Building file: ../src/GraphImageSegmentation.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/GraphImageSegmentation.d" -MT"src/GraphImageSegmentation.d" -o "src/GraphImageSegmentation.o" "../src/GraphImageSegmentation.cpp"
Finished building: ../src/GraphImageSegmentation.cpp
Building file: ../src/HelloOpenCV2.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/HelloOpenCV2.d" -MT"src/HelloOpenCV2.d" -o "src/HelloOpenCV2.o" "../src/HelloOpenCV2.cpp"
Finished building: ../src/HelloOpenCV2.cpp
Building file: ../src/HomographyWarp.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/HomographyWarp.d" -MT"src/HomographyWarp.d" -o "src/HomographyWarp.o" "../src/HomographyWarp.cpp"
Finished building: ../src/HomographyWarp.cpp
Building file: ../src/ImageManipulations.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/ImageManipulations.d" -MT"src/ImageManipulations.d" -o "src/ImageManipulations.o" "../src/ImageManipulations.cpp"
Finished building: ../src/ImageManipulations.cpp
Building file: ../src/InterestPoints.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/InterestPoints.d" -MT"src/InterestPoints.d" -o "src/InterestPoints.o" "../src/InterestPoints.cpp"
Finished building: ../src/InterestPoints.cpp
Building file: ../src/LinesAndComponents.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/LinesAndComponents.d" -MT"src/LinesAndComponents.d" -o "src/LinesAndComponents.o" "../src/LinesAndComponents.cpp"
Finished building: ../src/LinesAndComponents.cpp
Building file: ../src/MorphologicalManip.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/MorphologicalManip.d" -MT"src/MorphologicalManip.d" -o "src/MorphologicalManip.o" "../src/MorphologicalManip.cpp"
Finished building: ../src/MorphologicalManip.cpp
Building file: ../src/MultiviewColorConsistency.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/MultiviewColorConsistency.d" -MT"src/MultiviewColorConsistency.d" -o "src/MultiviewColorConsistency.o" "../src/MultiviewColorConsistency.cpp"
Finished building: ../src/MultiviewColorConsistency.cpp
Building file: ../src/StereoCamDepth.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/StereoCamDepth.d" -MT"src/StereoCamDepth.d" -o "src/StereoCamDepth.o" "../src/StereoCamDepth.cpp"
Finished building: ../src/StereoCamDepth.cpp
Building file: ../src/VideoProc.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/local/include -O0 -g3 -w -c -fmessage-length=0 -MMD -MP -MF"src/VideoProc.d" -MT"src/VideoProc.d" -o "src/VideoProc.o" "../src/VideoProc.cpp"
Finished building: ../src/VideoProc.cpp
Building target: HelloOpenCV2
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "HelloOpenCV2" ./src/segment/convolve.o ./src/segment/disjoint-set.o ./src/segment/filter.o ./src/segment/image.o ./src/segment/imconv.o ./src/segment/imutil.o ./src/segment/segment-graph.o ./src/segment/segment-image.o ./src/CameraPoseFromFundamentalMatrix.o ./src/GLViz.o ./src/GraphImageSegmentation.o ./src/HelloOpenCV2.o ./src/HomographyWarp.o ./src/ImageManipulations.o ./src/InterestPoints.o ./src/LinesAndComponents.o ./src/MorphologicalManip.o ./src/MultiviewColorConsistency.o ./src/StereoCamDepth.o ./src/VideoProc.o -lopencv_core -lGL -lGLU -lglut -lopencv_stitching -lopencv_contrib -lopencv_nonfree -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_flann -lopencv_imgproc -lopencv_highgui
../src/segment/filter.cpp:39: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/filter.cpp:40: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/filter.cpp:41: error: undefined reference to 'convolve_even(image<float>*, image<float>*, std::vector<float, std::allocator<float> >&)'
../src/segment/filter.cpp:42: error: undefined reference to 'convolve_even(image<float>*, image<float>*, std::vector<float, std::allocator<float> >&)'
../src/segment/filter.cpp:44: error: undefined reference to 'image<float>::~image()'
../src/segment/filter.cpp:52: error: undefined reference to 'imageUCHARtoFLOAT(image<unsigned char>*)'
../src/segment/filter.cpp:54: error: undefined reference to 'image<float>::~image()'
../src/segment/filter.cpp:63: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/imconv.cpp:15: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:31: error: undefined reference to 'image<rgb>::image(int, int, bool)'
../src/segment/imconv.cpp:46: error: undefined reference to 'image<float>::image(int, int, bool)'
../src/segment/imconv.cpp:73: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:90: error: undefined reference to 'void min_max<float>(image<float>*, float*, float*)'
../src/segment/imconv.cpp:97: error: undefined reference to 'image<long>::image(int, int, bool)'
../src/segment/imconv.cpp:110: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:127: error: undefined reference to 'void min_max<long>(image<long>*, long*, long*)'
../src/segment/imconv.cpp:135: error: undefined reference to 'image<unsigned char>::image(int, int, bool)'
../src/segment/imconv.cpp:152: error: undefined reference to 'void min_max<short>(image<short>*, short*, short*)'
../src/segment/segment-image.cpp:62: error: undefined reference to 'smooth(image<float>*, float)'
../src/segment/segment-image.cpp:63: error: undefined reference to 'smooth(image<float>*, float)'
../src/segment/segment-image.cpp:64: error: undefined reference to 'smooth(image<float>*, float)'
../src/segment/segment-image.cpp:65: error: undefined reference to 'image<float>::~image()'
../src/segment/segment-image.cpp:66: error: undefined reference to 'image<float>::~image()'
../src/segment/segment-image.cpp:120: error: undefined reference to 'image<rgb>::image(int, int, bool)'
../src/segment/segment-image.cpp:121: error: undefined reference to 'image<int>::image(int, int, bool)'
../src/segment/pnmfile.h:166: error: undefined reference to 'image<rgb>::image(int, int, bool)'
../src/segment/pnmfile.h:166: error: undefined reference to 'image<rgb>::image(int, int, bool)'
collect2: ld returned 1 exit status
make: *** [HelloOpenCV2] Error 1
18:54:30 Build Finished (took 8s.826ms)
I can also confirm that .o (object) files for each of the source files are generated. However, I do not understand how to fix the undefined reference error.
Would like to add that the file image.cpp / image.h contain a class definition. I have not added these 2 files as add->class have added them as add->source and add->header
Just applied "nm" command on image.o. Got an empty response. Although image.o is a 24 byte file.
image.h contains the template class declaration and image.cpp contains class function definition.
Image is a template class. The header and source for it are as -
/*
* image.h
*
* Created on: 25 Oct, 2014
* Author: eeuser
*/
#ifndef IMAGE_H_
#define IMAGE_H_
#include <cstring>
/* use imRef to access image data. */
#define imRef(im, x, y) (im->access[y][x])
/* use imPtr to get pointer to image data. */
#define imPtr(im, x, y) &(im->access[y][x])
template <class T>
class image {
public:
/* create an image */
image(int width, int height, bool init = true);
/* delete an image */
~image();
/* init an image */
void init(const T &val);
/* copy an image */
image<T> *copy() const;
/* get the width of an image. */
int width() const { return w; }
/* get the height of an image. */
int height() const { return h; }
/* image data. */
T *data;
/* row pointers. */
T **access;
private:
int w, h;
};
#endif /* IMAGE_H_ */
The source:
/*
* image.cpp
*
* Created on: 25 Oct, 2014
* Author: eeuser
*/
#include "image.h"
template <class T>
image<T>::image(int width, int height, bool init) {
w = width;
h = height;
data = new T[w * h]; // allocate space for image data
access = new T*[h]; // allocate space for row pointers
// initialize row pointers
for (int i = 0; i < h; i++)
access[i] = data + (i * w);
if (init)
memset(data, 0, w * h * sizeof(T));
}
template <class T>
image<T>::~image() {
delete [] data;
delete [] access;
}
template <class T>
void image<T>::init(const T &val) {
T *ptr = imPtr(this, 0, 0);
T *end = imPtr(this, w-1, h-1);
while (ptr <= end)
*ptr++ = val;
}
template <class T>
image<T> *image<T>::copy() const {
image<T> *im = new image<T>(w, h, false);
memcpy(im->data, data, w * h * sizeof(T));
return im;
}
I turned on the extra warning flags in g++ (-Wextra). It complained about a bunch of static functions I had. I just changed those to normal functions. I guess this is ok, since those functions were not associated with a class.
Related
My C++ file is as following:
class LogMessage {
public:
LogMessage(const char* file, int line)
:
#ifdef __ANDROID__
log_stream_(std::cout)
#else
log_stream_(std::cerr)
#endif
{
std::unique_lock<std::mutex> lk(mu_);
log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":"
<< line << ": ";
}
~LogMessage() { log_stream_ << "\n"; }
std::ostream& stream() { return log_stream_; }
protected:
std::ostream& log_stream_;
private:
DateLogger pretty_date_;
mutable std::mutex mu_;
LogMessage(const LogMessage&);
void operator=(const LogMessage&);
};
When I compile it on Ubuntu with
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/postoffice.cc -o build/postoffice.o,
I meet the following problem:
root#4ec615bfc8fa:~/hbsun/nccl/ps-lite# ./make.sh
rm -rf build tests/test_connection tests/test_simple_app tests/test_kv_app_multi_workers tests/test_kv_app_benchmark tests/test_kv_app tests/*.d
find src -name "*.pb.[ch]*" -delete
/home/hbsun/nccl/ps-lite/deps/bin/protoc --cpp_out=./src --proto_path=./src src/meta.proto
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/customer.o src/customer.cc >build/customer.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/postoffice.o src/postoffice.cc >build/postoffice.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/van.o src/van.cc >build/van.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/meta.pb.o src/meta.pb.cc >build/meta.pb.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/meta.pb.cc -o build/meta.pb.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/customer.cc -o build/customer.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/van.cc -o build/van.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/postoffice.cc -o build/postoffice.o
In file included from ./include/ps/internal/utils.h:6:0,
from ./include/ps/base.h:7,
from src/van.cc:8:
./include/dmlc/logging.h:189:8: error: 'mutex' in namespace 'std' does not name a type
std::mutex mu_;
^
./include/dmlc/logging.h: In constructor 'dmlc::LogMessage::LogMessage(const char*, int)':
./include/dmlc/logging.h:177:5: error: 'unique_lock' is not a member of 'std'
std::unique_lock<std::mutex> lk(mu_);
^
./include/dmlc/logging.h:177:22: error: 'mutex' is not a member of 'std'
std::unique_lock<std::mutex> lk(mu_);
^
./include/dmlc/logging.h:177:37: error: 'mu_' was not declared in this scope
std::unique_lock<std::mutex> lk(mu_);
^
./include/dmlc/logging.h:177:40: error: 'lk' was not declared in this scope
std::unique_lock<std::mutex> lk(mu_);
^
Makefile:52: recipe for target 'build/van.o' failed
make: *** [build/van.o] Error 1
make: *** Waiting for unfinished jobs....
rm src/meta.pb.h
.
It seems that I cannot use std::mutex, my g++ version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. and I use Ubuntu.
Probably best to add: #include <mutex>
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I have a library from a camera manufacturer, and in the demo code provided, a function called metadata_init() works fine, in my code though, I get an undefined reference error.
The make output from the demo code:
/opt/linaro-multilib-2013.09-gcc4.8/bin/arm-linux-gnueabihf-gcc -lm -g -L"/home/aro/Downloads" -o hicore demoS2.c -lpthread -lyuvlib -lrt
and thats it, the build succeeds fine, I will have a working ./hicore application in there.
My project is a little bit more complicated, and I compile using eclipse.
The console output is:
11:33:23 **** Build of configuration Camera-R4-Debug for project Camera ****
make all
Building file: ../src/Camera.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Camera.d" -MT"src/Camera.o" -o "src/Camera.o" "../src/Camera.cpp"
../src/Camera.cpp: In function ‘int metadata_construct_http_message(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*)’:
../src/Camera.cpp:379:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
"</svg>\r\n";
^
../src/Camera.cpp: In function ‘int main()’:
../src/Camera.cpp:426:97: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
(void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
^
Finished building: ../src/Camera.cpp
Building file: ../src/CameraFrameGrabber.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraFrameGrabber.d" -MT"src/CameraFrameGrabber.o" -o "src/CameraFrameGrabber.o" "../src/CameraFrameGrabber.cpp"
Finished building: ../src/CameraFrameGrabber.cpp
Building file: ../src/CameraLogger.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraLogger.d" -MT"src/CameraLogger.o" -o "src/CameraLogger.o" "../src/CameraLogger.cpp"
Finished building: ../src/CameraLogger.cpp
Building file: ../src/CameraParameter.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraParameter.d" -MT"src/CameraParameter.o" -o "src/CameraParameter.o" "../src/CameraParameter.cpp"
Finished building: ../src/CameraParameter.cpp
Building target: Camera
Invoking: Cross G++ Linker
arm-linux-gnueabihf-g++ -L/opt/Camerasdk/R4/lib -L"/cameraBuilds/Wrapper/Camera-R4-Debug" -L"/opt/ExternalLibraries/curl/Camera/R4/lib" -L"/opt/ExternalLibraries/libxml2/Camera/R4/lib" -L"/opt/ExternalLibraries/OpenCV24/Camera/R4/lib" -L"/opt/DetectionModules/Camera/R4/lib" -L/home/aro/Downloads -o "Camera" ./src/Camera.o ./src/CameraFrameGrabber.o ./src/CameraLogger.o ./src/CameraParameter.o -lWrapper -lxml2 -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lyuvlib -lrt -llibjasper -llibjpeg -llibpng -llibtiff -lzlib -lcurl
./src/Camera.o: In function `main':
/home/aro/cameraBuilds/Camera/Camera-R4-Debug/../src/Camera.cpp:426: undefined reference to `metadata_init(char*, int, int (*)(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*))'
collect2: error: ld returned 1 exit status
make: *** [Camera] Error 1
11:33:24 Build Finished (took 1s.212ms)
The code itself is the very same thing, I just copied it over.
Metadata.h:
#ifndef _HIK_METADATA_H_
#define _HIK_METADATA_H_
const int max_http_body_len = 100 * 1024;
typedef enum
{
CMD_ADD_TYPE = 1,
CMD_OTHER,
} METADATA_CTRL_TYPE;
typedef struct
{
int length;
int fd;
METADATA_CTRL_TYPE cmd_type;
} METADATA_HEADER;
typedef struct
{
char option[128];
int share_socket;
} METADATA_ADD_CFG;
typedef enum
{
HTTP_HEADER_TYPE = 1,
HTTP_BODY_TYPE,
} METADATA_HTTP_MESSAGE_TYPE;
typedef struct
{
char boundary[64];
char http_content_type[64];
char multipart_content_type[64];
} METADATA_MULTIPART_TYPE;
typedef int (*p_metadata_construct_http_msg_callback_f)(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len);
int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);
#endif
In Camera.cpp:
#include "Metadata.h"
int metadata_construct_http_message(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len)
{
// Removed for SO
return 0;
}
int main()
{
(void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
...
}
What causes this, and how can I debug this issue to narrow down how to fix it?
It think the problem could be that metadata_init is a C function, but you use it from a C++ code.
In case of this, extern "C" must be used in the header file, like this:
#ifdef __cplusplus
extern "C" {
#endif
// embed the whole contents of the header file here, I just put the function here for brevity
int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);
#ifdef __cplusplus
}
#endif
It is because of name mangling rules. Names are mangled differently in C and C++. With extern "C", you tell the compiler that the names inside should be used with "C" mangling.
C++ has to do a complex name mangling compared to C, because it has to embed almost all signature information to a name (all parameters type), while in C, the mangled name usually is the same as the function name, or there is a _ prepended.
We have gcc 4.2 for mac, and gcc 4.4 for linux. When I build the same code, I get the following undefinded symbol:
"MyClassNameSpecific1::MyClassNameSpecific1(int, int, int, className::class1 const&, className::class2 const&, int, int)", referenced from:
MyOtherClassName::mContainer() in MyOtherClassName.o
ld: symbol(s) not found for architecture x86_64
The code looks like this for MyClassName.h:
class MyClassNameSpecific1;
class MyClassNameSpecific2;
class MyClassNameSpecific1
{
public:
MyClassNameSpecific1(const string ¶m1);
virtual ~MyClassNameSpecific1() {}
}
class MyClassNameSpecific2: public classU::UData
{
public:
MyClassNameSpecific2(int width, int height, int breadth, const className::class1 &dType, const className::class2 &layout, int tWidth, int tHeight);
};
The MyClassName.cpp has this:
#include "MyClassName.h"
MyClassNameSpecific1::MyClassNameSpecific1(const string ¶m1) : classU::UData()
{
//does things here
}
MyClassNameSpecific2::MyClassNameSpecific2(int width, int height, int breadth, const className::class1 &dType, const className::class2 &layout, int tWidth, int tHeight) : classU::UData()
{
//does things here
}
I'm not sure what could cause the undef in one but not the other. Is anyone aware of a difference in gcc for this kind of situation? My makefile has all: MyClassName then other classes. I don't see any differences in the parameter signatures between .h and .cpp file for MyClassNameSpecific2.
I tried making sure I used the complete className::class1 in both the .h and .cpp files, but it still had the same undef. Also, I tried adding class MyClassNameSpecific2, where it only had class MyClassNameSpecific1 at the top of the MyClassName.h file, but it didn't change the undef. I tried googling the problem, but nothing pertinent turned up. Maybe there's another thing I could have searched for, but I'm not sure. I left out #includes for the little class definitions in my .h and .cpp file.
That would be great if anyone had something to try, even if it's a different nomenclature for the parameters that I didn't think of.
Added makefile:
.SUFFIXES: .cpp
DEP_DIR = ../dependencies
CC = g++
OS := $(shell uname -s)
ifeq ($(OS),Darwin)
#set LIB_DIR
LIB_DIR1=darwin64_gcc42/lib
LIB_DIR2=darwin64_gcc44/lib
else
LIB_DIR=linux64_gcc44/lib
endif
INCDIRS = -I. -I../include \
-I$(DEP_DIR)/className/include \
-I$(DEP_DIR)/classNameOther/include
#C++FLAGS = -c -fPIC -g -O2 -DLINUX -D_DEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall
C++FLAGS = -c -fPIC -O2 -DLINUX -DNDEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall
ifeq ($(OS),Darwin)
LDFLAGS = -m64 -pthread -ldl -shared -L../$(LIB_DIR1)/release \
-L$(DEP_DIR)/className/$(LIB_DIR1) \
-L$(DEP_DIR)/classNameOther/$(LIB_DIR2)/release
else
LDFLAGS = -m64 -pthread -ldl -shared -L../$(LIB_DIR)/release \
-L$(DEP_DIR)/className/$(LIB_DIR) \
-L$(DEP_DIR)/classNameOther/$(LIB_DIR)/release
endif
LDLIBS = -llittleClass -lclassName -lclassNameOther -lclassNameOthermalloc
all: MyClassName MyOtherClassName AnotherClass2 AnotherClass3 AnotherClass4 AMoreOverallClass
AMoreOverallClass: AMoreOverallClass.o
$(CC) AMoreOverallClass.o $(LDFLAGS) -o $# $(LDLIBS)
...
.cpp.o:
$(CC) $(C++FLAGS) $(INCDIRS) $< -o $#
clean:
rm -rf *.o all
Here is make output at command line:
mcle#engmacvi01(577)% make
g++ -c -fPIC -O2 -DLINUX -DNDEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall -I. -I../include -I../dependencies/className/include -I../dependencies/classNameOther/include MyClassName.cpp -o MyClassName.o
g++ MyClassName.o -m64 -pthread -ldl -shared -L../darwin64_gcc42/lib/release -L../dependencies/className/darwin64_gcc42/lib -L../dependencies/classNameOther/darwin64_gcc44/lib/release -o MyClassName-ludm -lclassName -lclassNameOther -lclassNameOthermalloc
g++ -c -fPIC -O2 -DLINUX -DNDEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall -I. -I../include -I../dependencies/className/include -I../dependencies/classNameOther/include AnotherClass.cpp -o AnotherClass.o
g++ AnotherClass.o -m64 -pthread -ldl -shared -L../darwin64_gcc42/lib/release -L../dependencies/className/darwin64_gcc42/lib -L../dependencies/classNameOther/darwin64_gcc44/lib/release -o AnotherClass -llittleClass -lclassName -lclassNameOther -lclassNameOthermalloc
Undefined symbols for architecture x86_64:
"MyClassNameSpecific1::MyClassNameSpecific1(int, int, int, className::class1 const&, className::class2 const&, int, int)", referenced from:
AnotherClass::mContainer() in AnotherClass.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [AnotherClass] Error 1
Also, MyOtherClassName.cpp:
UReturnClass &MyOtherClassName::mContainer()
{
if (!m_mContainerPtr)
{
m_mContainerPtr = new UReturnClass();
UMPtr md = new MyClassNameSpecific2(m_width, m_height, m_bands, m_dataType, m_dataLayout, m_tileWidth, m_tileHeight);
m_mContainerPtr->setMdata(md);
}
return *m_mContainerPtr;
}
MyOtherClassName.h:
className::class1 m_dType;
className::class2 m_dLayout;
The fix for this was to change the makefile so that it built it all in one step instead of separate steps for each class, since they were trying to link separately to get the .o file for each one, before putting it all together at the end. I'm not sure why the linux build was fine doing this separately and the mac build was not. Class names changed to protect the innocent.
SRCS=UMetaPlugin.cpp UDataPlugin.cpp UForPlugin.cpp UFacPlugin.cpp UMTransPlugin.cpp UPlugin.cpp
objects=$(patsubst %.cpp,%.o,$(SRCS))
all: $(appn)
$(appn): $(objects)
$(CC) $(LDFLAGS) -o $# $(objects) $(LDLIBS)
.cpp.o:
$(CC) $(C++FLAGS) $(INCDIRS) $< -o $#
I have problem with gdal.Iam currently working on Ubuntu and the IDE is Eclipse CDT i have given my code below and i have also installed the gdal package but it gives build errors
#include <stdio.h>
#include<unistd.h>
#include "gdal/gdal_priv.h"
#include "gdal/cpl_conv.h"
#include<gdal/gdal.h>
int main(int argc,char* argv[])
{
void *hLib_Comm;
char exePath[800];
if(getcwd(exePath,sizeof(exePath))== NULL) {
printf("Exe path access error\n");
}
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen( exePath, GA_ReadOnly );
if( poDataset == NULL )
{
printf("Exe path access error\n");;
}
}
///////////Bulid result///////////////////////////////////
14:51:47 **** Build of configuration Debug for project ReadFrmSoFile ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -lm -I/usr/include/gdal -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main(int, char**)’:
../main.cpp:17:8: warning: unused variable ‘hLib_Comm’ [-Wunused-variable]
Finished building: ../main.cpp
Building target: ReadFrmSoFile
Invoking: GCC C++ Linker
g++ -lm -L/usr/lib -L/usr/lib/ogdi -o "ReadFrmSoFile" ./main.o -lgdal
./main.o: In function `main':
/media/Local Disk/workspace2/ReadFrmSoFile/Debug/../main.cpp:24: undefined reference to `GDALAllRegister'
/media/Local Disk/workspace2/ReadFrmSoFile/Debug/../main.cpp:26: undefined reference to `GDALOpen'
collect2: ld returned 1 exit status
make: *** [ReadFrmSoFile] Error 1
//////////////////////////////////////////////
Please Help Me......
Well, first of all, sorry about my bad english!
I'm new to linux, g++ and perl, and I'm getting some problems here.
I have a code in G++ which calls a perl .pl file to return a information. Right now, I'm just returning 1 or 0 from the perl .pl file for tests and to understand how does it works. But the problem is that I'm getting this from the $make:
sathlervbn Spam C # make clean;make
rm -f *.o
g++ -Wall -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.14/CORE -c -o filedir.o filedir.cpp
g++ -Wall -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/perl/5.14/CORE -c -o main.o main.cpp
main.cpp: In function ‘int main(int, char**, char**)’:
main.cpp:112:41: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
main.cpp:112:41: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
g++ -L/usr/lib -Wall -Wl,-E -fstack-protector -L/usr/local/lib -L/usr/lib/perl/5.14/CORE - lperl -ldl -lm -lpthread -lc -lcrypt -o main filedir.o main.o
main.o: In function `getInfoPerl(std::string)':
main.cpp:(.text+0x1a): undefined reference to `Perl_push_scope'
main.cpp:(.text+0x33): undefined reference to `Perl_save_int'
main.cpp:(.text+0x73): undefined reference to `Perl_markstack_grow'
main.cpp:(.text+0xcd): undefined reference to `Perl_stack_grow'
main.cpp:(.text+0xfa): undefined reference to `Perl_newSVpv'
main.cpp:(.text+0x10d): undefined reference to `Perl_sv_2mortal'
main.cpp:(.text+0x13b): undefined reference to `Perl_call_pv'
main.cpp:(.text+0x18f): undefined reference to `Perl_sv_2iv_flags'
main.cpp:(.text+0x1bd): undefined reference to `Perl_free_tmps'
main.cpp:(.text+0x1ca): undefined reference to `Perl_pop_scope'
main.o: In function `main':
main.cpp:(.text+0x206): undefined reference to `Perl_sys_init3'
main.cpp:(.text+0x20b): undefined reference to `perl_alloc'
main.cpp:(.text+0x21d): undefined reference to `perl_construct'
main.cpp:(.text+0x265): undefined reference to `perl_parse'
main.cpp:(.text+0x272): undefined reference to `perl_run'
main.cpp:(.text+0x2fd): undefined reference to `perl_destruct'
main.cpp:(.text+0x30a): undefined reference to `perl_free'
main.cpp:(.text+0x30f): undefined reference to `Perl_sys_term'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
The main.cpp code is:
#include <EXTERN.h>
#include <perl.h>
#include <iostream>
#include <cstdio>
#include "filedir.h"
using namespace std;
PerlInterpreter *my_perl;
int getInfoPerl(string email){
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(email.c_str(),0)));
PUTBACK;
call_pv("spamTeste", G_SCALAR);
SPAGAIN;
int resultado = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return resultado;
}
int main(int argc, char **argv, char **env) {
char *my_argv[] = { " ", "spamPerl.pl" };
PERL_SYS_INIT3 (&argc, &argv, &env);
my_perl = perl_alloc();
perl_construct ( my_perl );
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL);
perl_run(my_perl);
cout << "Resultado " << getInfoPerl("email/email.txt") << endl;
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
foobar bla bla bla another part from the code: doesn't matter.
}
Here is the makefile:
#CC= /usr/bin/g++
CPP = g++
CPPFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ccopts)
#LD= /usr/bin/g++
LD = g++
LFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ldopts)
#LFLAGS = -Wall -Wl,-E -fstack-protector -L/usr/local/lib -L/usr/lib/perl/5.14/CORE -lperl -ldl -lm -lpthread -lc -lcrypt
MAINOBJS = filedir.o main.o
EMAILS = main
EXECS = $(EMAILS)
#Regra Implicita:
.c.o:
$(CPP) $(CPPFLAGS) -c $<
all: emails
emails: $(EMAILS)
main: $(MAINOBJS)
$(LD) -L/usr/lib $(LFLAGS) -o $# $(MAINOBJS)
clean:
rm -f *.o
What I did? I've tried installing libperl-dev package, update the perl, and nothing solved.
I really need to fix this! Can someone help me?
UPDATE:
Changed the Header from main.cpp to:
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#ifdef __cplusplus
}
#endif
#include <iostream>
#include <cstdio>
#include "filedir.h"
Didn't work...
The answer previous is right, it is cause by the sequence of gcc parameters. I test a sample code provided by the official perlembed tutorial:
http://perldoc.perl.org/perlembed.html
if the compile option is
cc -o interp interp.c perl -MExtUtils::Embed -e ccopts -e ldopts
as provided by the tutorial, it is right.
But any other sequence is wrong, for example:
cc interp.c -o interp perl -MExtUtils::Embed -e ccopts -e ldopts
cc perl -MExtUtils::Embed -e ccopts -e ldopts interp.c -o interp
so make sure in your Makefile -o goes the first and source file goes the second.
The problem was in the makefile:
#CC= /usr/bin/g++
CPP = g++
CPPFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ccopts)
#LD= /usr/bin/g++
LD = g++
LFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ldopts)
#LFLAGS = -Wall -Wl,-E -fstack-protector -L/usr/local/lib -L/usr/lib/perl/5.14/CORE -lperl -ldl -lm -lpthread -lc -lcrypt
MAINOBJS = filedir.o main.o
EMAILS = main
EXECS = $(EMAILS)
#Regra Implicita:
.c.o:
$(CPP) $(CPPFLAGS) -c $<
all: emails
emails: $(EMAILS)
main: $(MAINOBJS)
$(LD) -L/usr/lib $(LFLAGS) -o $# $(MAINOBJS)
clean:
rm -f *.o
As you can see, in this line, the code:
main: $(MAINOBJS)
$(LD) -L/usr/lib $(LFLAGS) -o $# $(MAINOBJS)
Should have the $(LFLAGS) after the $(MAINOBJS), so it should be:
main: $(MAINOBJS)
$(LD) -L/usr/lib -o $# $(MAINOBJS) $(LFLAGS)
Now, the linker is working perfectly. I'm sorry, but I can't say exactly why this is necessary, I've just discovered.