Linker Error: Undefined Reference to Function - c++11

I'm running into a linker error when I try to compile my code.
When I compile using g++ *.cpp -std=c++11 -o run I get the following error:
main.cpp:(.text+0x355): undefined reference to `Actions(mBoard&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)'
collect2: error: ld returned 1 exit status
I've tried compiling and linking all the files separately to no avail. Its not a member function so it's not a label issue. I've also tried to use g++ -std=c++11 main.cpp mAI.cpp -o run to make sure it is in fact compiling and linking both files but no luck.
What's really making me crazy is that it's not complaining about the Translate function that's declared and defined in the same way as the Actions function.
Any help is greatly appreciated.
mAI.hpp
#ifndef MAI_HPP
#define MAI_HPP
#include <iostream>
#include <set>
#include <tuple>
#include "mBoard.hpp"
using namespace std;
void Actions(mBoard const &state, string const playerColor, set<string> moveList);
tuple<int, int> Translate(string index);
#endif
mAI.cpp
#include "mAI.hpp"
std::set<char> blackPieces = {'r', 'n', 'b', 'q', 'k', 'p'};
std::set<char> whitePieces = {'R', 'N', 'B', 'Q', 'K', 'P'};
void Actions(mBoard const &state, string const playerColor, set<string> moveList)
{
//Do some stuff
}
tuple<int, int> Translate(string index)
{
//Do different stuff
}
main
#include "mAI.hpp"
#include "mBoard.hpp"
#include <set>
#include <tuple>
#include <string>
#include <iostream>
int main()
{
mBoard dasBoard;
set<string> moveList;
string color = "White";
cout<<"TEST - Translate: f6 to file, rank: ";
tuple<int, int> test;
test = Translate("f6");
cout << get<0>(test) << ", " << get<1>(test) << endl;
Actions(dasBoard, color, moveList);
return 0;
}

That sounds like a very old gcc compiler. That was a problem during abi change in earlier versions of gcc and installed libraries on the system.
using -D_GLIBCXX_USE_CXX11_ABI=0 and recompile all maybe help.
See also this documentation:
https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.html
BTW:
I can compile and link your code with gcc 7.3.1 ( fedora ) without any error. Some warnings of unused vars after cut and paste your code and only add an empty aBoard class. So I believe there is nothing wrong with your code.
From the documentation:
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. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.

Related

Boost::Process linker error in combination with boost::process::posix::use_vfork

I am toying around with Boost::Process (1.64.0), using GCC 7.1.1, trying to force the use of vfork() when forking a process. I am greeted by a linker error for the following program:
#include <boost/process.hpp>
namespace bp = ::boost::process;
int main(void)
{
bp::child c("ls", bp::posix::use_vfork);
c.wait();
return 0;
}
Using:
g++ use_vfork.cpp
The linker error generated is as follows
In function 'boost::process::detail::posix::executor >, boost::fusion::filter_view&, boost::process::detail::posix::use_vfork_ const&> const, boost::process::detail::is_initializer > > > >::operator()()':
spawn_simple.cpp:(.text._ZN5boost7process6detail5posix8executorINS_6fusion10joint_viewINS4_5tupleIJNS2_12exe_cmd_initIcEEEEENS4_11filter_viewIKNS6_IJRA6_KcRNS2_8null_outILi1ELin1EEERKNS2_10use_vfork_EEEENS1_14is_initializerIN4mpl_3argILin1EEEEEEEEEEclEv[_ZN5boost7process6detail5posix8executorINS_6fusion10joint_viewINS4_5tupleIJNS2_12exe_cmd_initIcEEEEENS4_11filter_viewIKNS6_IJRA6_KcRNS2_8null_outILi1ELin1EEERKNS2_10use_vfork_EEEENS1_14is_initializerIN4mpl_3argILin1EEEEEEEEEEclEv]+0x31): undefined reference to `boost::process::detail::posix::executor >, boost::fusion::filter_view&, boost::process::detail::posix::use_vfork_ const&> const, boost::process::detail::is_initializer > > > >::invoke(mpl_::bool_, mpl_::bool_)'
As far as I know, Boost Process itself is header only, so why is it complaining about an undefined reference of boost::process<...>::invoke()?
I tried adding -lboost_system and -lboost_iostreams, but that does not solve the linker error.
It could be a documentation bug/omission: vfork support is conditionally compiled-in:
#define BOOST_POSIX_HAS_VFORK 1
So this works:
#include <boost/process.hpp>
namespace bp = ::boost::process;
int main(void)
{
bp::child c("ls", bp::posix::use_vfork);
c.wait();
return 0;
}

gtkmm undefined reference to certain gtk::builder function add_from_file

I am using eclipse, mingw-w64, gtkmm2.4, glade to compile some simple program.
I can compile hello world gtkmm examples, following a tutorial to, however when it comes to glade came a little strange undefined to error.
program that compiled and run smoothly, which was the gtkmm 2.24 simple example tutorial https://developer.gnome.org/gtkmm-tutorial/2.24/sec-basics-simple-example.html.en
#include <gtkmm.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Main::run(*window);
return 0;
}
however when I try to run another simple example from the glade chapter (24.2.1) things does not work out.
https://developer.gnome.org/gtkmm-tutorial/2.24/sec-builder-accessing-widgets.html.en
example:
#include <gtkmm.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
//Gtk::Window window; //I changed it to fit with the glade example
Gtk::Window* window; //I changed this line from the example
//////////////// this part was pretty much just copied out from the example//////
Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
try
{
refBuilder->add_from_file("something.glade"); //this is the line with problem.
}
catch(const Glib::FileError& ex)
{
std::cerr << "FileError: " << ex.what() << std::endl;
return 1;
}
catch(const Gtk::BuilderError& ex)
{
std::cerr << "BuilderError: " << ex.what() << std::endl;
return 1;
}
refBuilder->get_widget("window1", window);
//////////////// end of copied out from the example//////
Gtk::Main::run(*window);
return 0;
}
when compiled, it gave error as follow
test.cpp:(.text.startup+0x281): undefined reference to Gtk::Builder::add_from_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
It seems to take the argument "something.glade" as type std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& (which I have no idea what it is).
according to the gtkmm manual (https:developer.gnome.org/gtkmm/stable/classGtk_1_1Builder.html#aa3f4af4e7eaf7861c8283dc0dbd5254c) seems it only takes Gtk::Builder::add_from_file(const std::string & filename). So is the argument type really the problem?
I have tried casting it as std::string by doing std::string() or string(), but it gave the same error.
I have tried commenting out the line in question, and it compiled fine.
Gtk::Builder::create() did not receive undefined reference to compilation error
refBuilder->get_widget("window1", window); did not receive undefined reference to compilation error
So now I am scratching my head all over this seems trivial issue. Please provide some help.
For more information
pkg-config --cflags --libs yielded -IE:/gtkmm64/include/gtkmm-2.4 -IE:/gtkmm64/lib/gtkmm-2.4/include -IE:/gtkmm64/include/atkmm-1.6 -IE:/gtkmm64/include/giomm-2.4 -IE:/gtkmm64/lib/giomm-2.4/include -IE:/gtkmm64/include/pangomm-1.4 -IE:/gtkmm64/lib/pangomm-1.4/include -IE:/gtkmm64/include/gtk-2.0 -IE:/gtkmm64/include/gdkmm-2.4 -IE:/gtkmm64/lib/gdkmm-2.4/include -IE:/gtkmm64/include/atk-1.0 -IE:/gtkmm64/include/glibmm-2.4 -IE:/gtkmm64/lib/glibmm-2.4/include -IE:/gtkmm64/include/glib-2.0 -IE:/gtkmm64/lib/glib-2.0/include -IE:/gtkmm64/include/sigc++-2.0 -IE:/gtkmm64/lib/sigc++-2.0/include -IE:/gtkmm64/include/cairomm-1.0 -IE:/gtkmm64/lib/cairomm-1.0/include -IE:/gtkmm64/include/pango-1.0 -IE:/gtkmm64/include/cairo -IE:/gtkmm64/include -IE:/gtkmm64/include/freetype2 -IE:/gtkmm64/include/libpng14 -IE:/gtkmm64/lib/gtk-2.0/include -IE:/gtkmm64/include/gdk-pixbuf-2.0 -LE:/gtkmm64/lib -lgtkmm-2.4 -latkmm-1.6 -lgdkmm-2.4 -lgiomm-2.4 -lpangomm-1.4 -lgtk-win32-2.0 -lglibmm-2.4 -lcairomm-1.0 -lsigc-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpng14 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl, Which I have included into the project
pkg-config --modversion gtkmm2.4 yielded 2.22.0, so I doubt it has anything to do with the 2.14 requirement for add_from_file()
pkg-config --modversion gtk+2.0 yielded 2.22.0
I did not use any flags like --std=c++xx.
windows 8 64bit
seems Converting std::__cxx11::string to std::string solved it.
put #define _GLIBCXX_USE_CXX11_ABI 0 at start because I am using gcc 5.

xilinx sdka error when using lwip library [duplicate]

I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX's semaphore tutorial and changed things around to their syntax and as a result am now getting these reference errors.
I may simply be overlooking something, but I cannot find it.
Errors:
Producers_Consumers.c:52: warning: return type of ‘main’ is not ‘int’
/tmp/cceeOM6F.o: In function `producer':
Producers_Consumers.c:(.text+0x1e): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x3a): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x46): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x52): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x5e): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0x6a): undefined reference to `sem_post'
/tmp/cceeOM6F.o: In function `consumer':
Producers_Consumers.c:(.text+0x7e): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x8a): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x96): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0xa2): undefined reference to `sem_post'
collect2: ld returned 1 exit status
What I have (It may look a bit ugly due to the way I commented things out from my old method) I also know my adding method won't work, but I'll get to that when I fix my syntax issues:
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#define N 10 //Number of slots in buffer
typedef int semaphore; //Semaphores ae a special kind of int
sem_t mutex; //Controls access to critical region 1
sem_t empty; //Counts empty buffer slots N
sem_t full; //Counts full buffer slots 0
int count = 0; //What we're putting in
//int buffer[N];
void producer(void) {
sem_init(&mutex, 0, 1);
//sem_init(&empty, 0, N);
sem_init(&full, 0, 0);
while(1) {
sem_wait(&empty);
sem_wait(&mutex);
//printf("Empy: %d\n",empty);
//printf("Mutex: %d\n",mutex);
//printf("Both Downs Ran\n");
//buffer = buffer + 1;
sem_post(&mutex);
sem_post(&full);
//printf("Producer produced: %d\n",buffer);
}
}
void consumer(void) {
while(1) {
sem_wait(&full);
sem_wait(&mutex);
//item = buffer;
sem_post(&mutex);
sem_post(&empty);
//printf("Consumer consumed: %d/n",item);
}
}
void main() {
}
If you are on a linux system, you'll need to compile and link with the -pthread flag to link the pthreads library.
gcc -pthread Producers_Consumers.c
As Paul Griffiths has pointed out, you can also use -lrt, which is more portable, and links the POSIX Realtime Extensions library
gcc Producers_Consumers.c -lrt
Other notes specific to the code in the question:
int main(void) not void main()
typedef int semaphore is wrong, sem_t should be treated as an opaque type, you never use this typedef in your code anyway.
A problem I foresee is that your consumer code uses the semaphores before they are initialized in producer. You should initialize them in your main
Got same error in ubuntu qt.
After adding
LIBS += -lpthread -lrt
to project.pro file all compiled fine.

Receive error: undefined reference to static member while I have defined them in classname.cpp

I am trying to define static members in a class and always received error undefined reference to static members.
I realize there are already many similar questions. But It seems that in those questions errors are raised because they did not define static members somewhere outside the class. I am sure that I have defined these static members.
The following is the problematic part of my code.
In foo.h, I defined a class named foo.
#include <random>
#include <vector>
class foo
{
public:
int random = dist(mt);
static std::random_device rd;
static std::mt19937 mt;
static std::uniform_int_distribution<int> dist;
static std::vector<int> ans(const int &);
};
In foo.cpp, there are definitions of static members.
#include<random>
#include<vector>
#include "foo.h"
std::random_device foo::rd;
std::uniform_int_distribution<int> foo::dist(0, 10);
std::mt19937 foo::mt(rd());
std::vector<int> foo::ans(const int & size) {
std::vector<int> bb;
for(int i=0; i < size; ++i)
bb.push_back(dist(mt));
return bb;
}
I used the class in another cpp file named test.cpp
#include <random>
#include <vector>
#include <iostream>
#include "foo.h"
int main()
{
foo a;
std::vector<int> c=a.ans(5);
for(auto it=c.begin(); it!=c.end(); ++it)
std::cout<<*it<<"\t";
std::cout<<"\n";
}
With g++ test.cpp -o a.out, I always received:
/tmp/ccUPnAxJ.o: In function `main':
test.cpp:(.text+0x37): undefined reference to `foo::ans(int const&)'
/tmp/ccUPnAxJ.o: In function `foo::foo()':
test.cpp:(.text._ZN3fooC2Ev[_ZN3fooC5Ev]+0xd): undefined reference to `foo::mt'
test.cpp:(.text._ZN3fooC2Ev[_ZN3fooC5Ev]+0x12): undefined reference to `foo::dist'
collect2: error: ld returned 1 exit status'
My g++ version is: g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2, and g++ is an alias to g++ -std=c++11.
You need:
g++ test.cpp foo.cpp -o a.out
Otherwise, how would g++ know about foo.cpp at all? It doesn't magically guess based on seeing #include "foo.h".

Some problems when using Boost Library on Xcode

I use Xcode 4.5.2 and I wonna use Boost, but I got some problems.
In Build Setting, if I choose libc++ (LLVM C++ standard library with C++11 support), I will get the error messgae "Apple Mach-O Linker (ld) Error".
Just like this:
Undefined symbols for architecture x86_64:
"boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::codecvt<wchar_t, char, __mbstate_t> const&)"
referenced from:
boost::filesystem::path::path<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, void>::type*) in test1 - inverted index.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 include these two headers:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
I also add these in Build Phases:
libboost_filesystem-mt.dylib
libboost_filesystem-mt.a
libboost_system-mt.dylib
libboost_system-mt.a
Code is here:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace boost::filesystem;
std::map<std::string, std::set<int>> invertedIndex;
std::map<std::string, int> number;
bool check_char(const char in)
{
if((in>='A' && in<='Z') || (in>='a' && in<='z'))
return true;
else
return false;
}
int main() {
int con = 0;
std::string word;
path root("/Users/tomhu/Desktop/pro/data/");
std::string rootDirectory = root.native();
recursive_directory_iterator iter(root);
recursive_directory_iterator end;
for (; iter != end; ++iter)
{
if(is_regular_file(*iter))
{
std::string filename;
std::string directory(rootDirectory);
filename = iter->path().filename().native();
directory.append(filename);
std::ifstream fileIn(directory.c_str());
number[filename] = con;
if(!fileIn)
{
std::cerr << "File doesn't exist!" << std::endl;
exit(1);
}
while(fileIn>>word)
{
long po = 0;
if(!check_char(*(word.end()-1)))
word.pop_back();
transform(word.begin(),word.end(),word.begin(),::tolower);
if(word=="i")
word="I";
invertedIndex[word].insert(con);
}
}
}
std::cout << con << std::endl;
return 0;
}
!!!!!!!!
If I choose libstdc++ (GUN C++ standard library) in "Build Setting", I won't get any error message about Boost, but I can't use anything in C++11 Standard.
How to solve these problems?
See Why can't clang with libc++ in c++0x mode link this boost::program_options example? - Howard gave an excellent answer what's going wrong there.
And here is how you can recompile Boost for clang+libc++: How to compile/link Boost with clang++/libc++?

Resources