Got an 'Undefined Symbols' error when compiling .cpp file with ginac - c++

I'm using gcc(version 11) to compile my .cpp file, and the code is below, which is copied from the official website of ginac.
#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;
int main()
{
symbol x("x"), y("y");
ex poly;
for (int i=0; i<3; ++i)
poly += factorial(i+16)*pow(x,i)*pow(y,2-i);
cout << poly << endl;
return 0;
}
I used the compiling command like this:
gcc-11 -lstdc++ -lginac -lcln hello.cpp -o hello -I /usr/local/include -L /usr/local/lib
After that, I got some errors like this:
Undefined symbols for architecture x86_64:
"__ZN5GiNaC12archive_node6add_exERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_2exE", referenced from:
__ZNK5GiNaC9containerISt6vectorE7archiveERNS_12archive_nodeE in ccEkVJyo.o
"__ZN5GiNaC5basic12read_archiveERKNS_12archive_nodeERNS_9containerINSt7__cxx114listEEE", referenced from:
__ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
"__ZN5GiNaC6symbolC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
_main in ccEkVJyo.o
"__ZN5GiNaC9containerISt6vectorE8reg_infoE", referenced from:
__ZN5GiNaC9containerISt6vectorE21get_class_info_staticEv in ccEkVJyo.o
"__ZN5GiNaClsERSoRKNS_2exE", referenced from:
_main in ccEkVJyo.o
"__ZNK5GiNaC12archive_node14find_ex_by_locEN9__gnu_cxx17__normal_iteratorIPKNS0_8propertyESt6vectorIS3_SaIS3_EEEERNS_2exERNS_9containerINSt7__cxx114listEEE", referenced from:
__ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
"__ZNK5GiNaC12archive_node19find_property_rangeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_", referenced from:
__ZN5GiNaC9containerISt6vectorE12read_archiveERKNS_12archive_nodeERNS0_INSt7__cxx114listEEE in ccEkVJyo.o
"__ZNK5GiNaC5basic10eval_ncmulERKSt6vectorINS_2exESaIS2_EE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic11to_rationalERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic13contract_withEN9__gnu_cxx17__normal_iteratorIPNS_2exESt6vectorIS3_SaIS3_EEEES8_RS7_", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic13to_polynomialERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic14subs_one_levelERKSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEEj", referenced from:
__ZNK5GiNaC9containerISt6vectorE4subsERKSt3mapINS_2exES4_NS_10ex_is_lessESaISt4pairIKS4_S4_EEEj in ccEkVJyo.o
"__ZNK5GiNaC5basic5matchERKNS_2exERSt3mapIS1_S1_NS_10ex_is_lessESaISt4pairIS2_S1_EEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC5basic6normalERSt3mapINS_2exES2_NS_10ex_is_lessESaISt4pairIKS2_S2_EEES9_RNS_9containerINSt7__cxx114listEEE", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
"__ZNK5GiNaC9containerISt6vectorE4infoEj", referenced from:
__ZTVN5GiNaC9containerISt6vectorEE in ccEkVJyo.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I know that Undefined Symbols error often comes up because of the link error, but the detailed error messages looks like that these errors are from ginac itself.
I have tried different orders of these -l parameters, but the errors always exist.
I tried clang instead of gcc to compile this file and it works. So I guess I do install the ginac library correctly. But I'm still confused about these errors when using gcc.
Has anyone met this problem?
I would be very appreciate if anyone could give me some advice!

I also had issues using:
gcc-11 -lstdc++ -lginac -lcln hello.cpp -o hello -I /usr/local/include -L /usr/local/lib
but it did work with:
g++ -o hello hello.cpp `pkg-config --cflags --libs ginac`

Related

fail to compile with SDL: Undefined symbols for architecture x86_64 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm trying to compile using SDL2 library with no success.
OS: MacOS 10.13.2
Visual Studio Code 1.43.1
Language: c++
I'm using the Lazy Foo's very basic example code.
This is the message I recieve:
[Running] cd "/Users/martinjoselizondocolomes/Programacion/01_hello_SDL/" && g++ 01_hello_SDL.cpp -o 01_hello_SDL && "/Users/martinjoselizondocolomes/Programacion/01_hello_SDL/"01_hello_SDL
Undefined symbols for architecture x86_64:
"_SDL_CreateWindow", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_Delay", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_DestroyWindow", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_FillRect", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_GetError", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_GetWindowSurface", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_Init", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_MapRGB", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_Quit", referenced from:
_main in 01_hello_SDL-a16d96.o
"_SDL_UpdateWindowSurface", referenced from:
_main in 01_hello_SDL-a16d96.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Done] exited with code=1 in 0.168 seconds
Building C++ code requires three stages:
Preprocessor deals with macros and #include directives, so your header files are required at this step.
Compiler generates object files. This is the point when the syntax is checked in your source files.
Linker combines object files and libraries into an executable so your runtime libraries must be available and specified to the linker.
For g++, you specify libraries by giving their full name on the command line or use the -l shortcut which figures out the proper prefix (lib) and suffix. So in your case I believe you can add -lSDL.
EDIT: If that doesn't work, check the output of
sdl-config --libs

Build Error when compiling c++ code which includes MATLAB Engine Library

I have Simple c++ Code that works fine while also using #include "engine.h" for the use of MATLAB in the c++ code.I am also using some functions like mxArray(), mxCreateDoubleMatrix() & mxSetPr(). After going to terminal and compiling the code with g++ -o main main.cpp the following errors occur
Undefined symbols for architecture x86_64:
"_engClose", referenced from:
_main in main-438cf4.o
"_engEvalString", referenced from:
_main in main-438cf4.o
"_engOpen", referenced from:
_main in main-438cf4.o
"_engPutVariable", referenced from:
_main in main-438cf4.o
"_mxCreateDoubleMatrix_730", referenced from:
_main in main-438cf4.o
"_mxSetPr", referenced from:
_main in main-438cf4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I don't think I need to put my actual code here since I am assuming this error has to do with compatibility issues not the code itself. If i'm wrong I will edit the post
P.S. I am using os Sierra. ( i have also tried compiling with xcode 9.0.1, same errors occur)
Thanks

Undefined symbols for architecture x86_64: linking mistake?

I am new to cpp, want to have a implementation of particle filter, I try to run the code here https://github.com/NewProggie/Particle-Filter, which is a structured and easy understanding project. But when I try to compile and link:
g++ $(pkg-config --cflags --libs opencv) -I/usr/local/Cellar/opencv3/3.1.0_1/include -I /usr/local/Cellar/gsl/1.16/include -stdlib=libc++ main.cpp -o main
I have following linking problem:
Undefined symbols for architecture x86_64:
"colorFeatures::colorFeatures()", referenced from:
_main in main-2b4c23.o
"colorFeatures::~colorFeatures()", referenced from:
_main in main-2b4c23.o
"adaboostDetect::detectObject(_IplImage*, CvRect**)", referenced from:
_main in main-2b4c23.o
"adaboostDetect::adaboostDetect()", referenced from:
_main in main-2b4c23.o
"tracker::addObjects(_IplImage*, CvRect*, int)", referenced from:
_main in main-2b4c23.o
"tracker::initTracker(_IplImage*, CvRect*, int, int)", referenced from:
_main in main-2b4c23.o
"tracker::showResults(_IplImage*)", referenced from:
_main in main-2b4c23.o
"tracker::next(_IplImage*)", referenced from:
_main in main-2b4c23.o
"tracker::tracker()", referenced from:
_main in main-2b4c23.o
"tracker::~tracker()", referenced from:
_main in main-2b4c23.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any kind person has ideas about this problem? Thanks in advance
have gsl installed properly
B) pass to g++ a reference to the lib directory where the gsl libraries are located (probably something like /usr/lib or /usr/local/lib, these should both be default locations for the linker to search), and also to where the header files are, and also tell the linker to do the linking.
g++ -o <name of executable> -L/path/to/gsl/libs -I/path/to/headers -lgsl <name of source file>
the -L tells it where to find the libraries (.so files on linux, .dylib on OS X), -I tells it where to find the headers, -l (that's a lower case L) tells it to link to the library, which would be named libgsl.so or libgsl.dylib.
First just try adding the -lgsl flag, then if it can't find libgsl.so (or .dylib), add the -L flag. NOTE: /path/to/gsl/libs and /path/to/headers are not what you should literally put in there, but replace them with the actual paths on your system.

Can't compile c++ file from terminal [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am using OpenCV to record the video from webcam. I used Xcode 6.4 to write a (C++) code to record the video on OS X Yosemite 10.10.4. It is working perfectly fine when I compile and run it from Xcode but when I want to compile (using g++ -o main main.cpp) it from terminal it generates following errors:
Undefined symbols for architecture x86_64:
"cv::VideoWriter::write(cv::Mat const&)", referenced from:
_main in main-027833.o
"cv::VideoWriter::VideoWriter(cv::String const&, int, double, cv::Size_<int>, bool)", referenced from:
_main in main-027833.o
"cv::VideoWriter::~VideoWriter()", referenced from:
_main in main-027833.o
"cv::VideoCapture::VideoCapture(int)", referenced from:
_main in main-027833.o
"cv::VideoCapture::~VideoCapture()", referenced from:
_main in main-027833.o
"cv::VideoCapture::operator>>(cv::Mat&)", referenced from:
_main in main-027833.o
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in main-027833.o
"cv::String::deallocate()", referenced from:
cv::String::~String() in main-027833.o
"cv::String::allocate(unsigned long)", referenced from:
cv::String::String(char const*) in main-027833.o
"cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
_main in main-027833.o
"cv::waitKey(int)", referenced from:
_main in main-027833.o
"cv::fastFree(void*)", referenced from:
cv::Mat::~Mat() in main-027833.o
"cv::VideoCapture::get(int) const", referenced from:
_main in main-027833.o
"cv::VideoCapture::isOpened() const", referenced from:
_main in main-027833.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command fail
ed with exit code 1 (use -v to see invocation)
I know its because it can't link OpenCV lib. I tried using "vi
$HOME/.bash_profile" and then setting path like
"export PATH=$PATH:/usr/local/include"
and
"export PATH=$PATH:/user/local/lib"
but it doesn't help. Also I tried
g++ -o main main.cpp -l `pkg-config opencv --cflags --libs`
but of no use.
I don't know how to specify path. Any help would be appreciated.
Thanks!
Finally, after 3 days of frustration I found the solution. I am sharing for someone who is also struggling with the same kind of problem.
What you have to do is just add all those flags that you added in the Xcode "other linker flags" while compiling your code from terminal. So here is the command that works for me.
g++ main.cpp -o main -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_calib3d -lopencv_videoio -lopencv_video -lopencv_videostab -lopencv_highgui
I didn't include all the flags because I was only working with videos but you may have to add more or less depending on what you are working on.

g++ ld: symbol(s) not found for architecture x86_64

I'm trying to compile the Sam Hare's Struck code.
I'm using mac OSX10.9, opencv 2.4.6 and Eigen 2.0.17.
Eigen and opencv headers are stored in /opt/local/include while opencv dylib in /opt/local/lib.
I modified the Hare's Makefile to work on this folder. When I type make on the terminal:
g++ -L/opt/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc src/Config.o src/Features.o src/HaarFeature.o src/HaarFeatures.o src/HistogramFeatures.o src/ImageRep.o src/LaRank.o src/MultiFeatures.o src/RawFeatures.o src/Sampler.o src/Tracker.o src/main.o src/GraphUtils/GraphUtils.o -o struck
I get these errors:
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
int)", referenced from:
_main in main.o "cv::split(cv::Mat const&, std::__1::vector<cv::Mat, std::__1::allocator<cv::Mat> >&)",
referenced from:
ImageRep::ImageRep(cv::Mat const&, bool, bool, bool) in ImageRep.o "cv::imread(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
int)", referenced from:
_main in main.o "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&,
cv::_InputArray const&)", referenced from:
LaRank::Debug() in LaRank.o
Tracker::Debug() in Tracker.o
_main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see
invocation)
Any ideas? Thanks!
I had a similar warning/error/failure when I was simply trying to make an executable from two different object files (main.o and add.o). I was using the command:
gcc -o exec main.o add.o
But my program is a C++ program. Using the g++ compiler solved my issue:
g++ -o exec main.o add.o
I was always under the impression that gcc could figure these things out on its own. Apparently not. I hope this helps someone else searching for this error.
finally solved my problem.
I created a new project in XCode with the sources and changed the C++ Standard Library from the default libc++ to libstdc++ as in this and this.
I am getting the same error. I don't think it is a "fix", but my work-around is to also include the cpp file. So instead of just putting
#include "MyClass.h"
I have to put
#include "MyClass.h"
#include "MyClass.cpp"
I wrote my declarition in the .h file
class String
{
String (const char * cstr = 0);
};
and I used inline in another .cpp file (implementation)
inline String::String(const char * cstr)
{
//code ...
}
then I use g++ and get this:
Undefined symbols for architecture x86_64:
"String::String(char const*)", referenced from:
_main in test-d389f3.o
ld: symbol(s) not found for architecture x86_64
solution: do not write inline when declarition and implementation is independent.
When I using clang to compile a C++ program, I have the similar error.
But after I changing to use chang++, the compilation worked.