I just installed CppUTest on my MAC using brew as indicated by the guide.
It failed when I tried to build the example cpp.
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest)
{
FAIL("Fail me!");
}
I guess it is because the header file which define those macros are not included. So I add include as below:
#include "CppUTest/TestHarness.h"
#include "CppUTest/TestOutput.h"
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest)
{
FAIL("Fail me!");
}
Now I get bunch of errors.
Undefined symbols for architecture x86_64: "UtestShell::assertTrue(bool, char const*, char const*, char const*, int)", referenced from: vtable for TEST_FirstTestGroup_FirstTest_TestShellin ccNDwnbv.o
The error you are getting is a linker error and it suggests that you are not linking the CppUTest library. It is hard to actually say what is wrong because your question is missing the Makefiles. Could you explain how you have compiled the example?
Related
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.
Goal: I want to use thread STL of C++11 in Matlab mex file (R2013a) using Xcode 4.6
I modified ~/.matlab/R2013a/mexopts.sh
CC='clang++' # was llvm-gcc-4.2
CXX='clang++' # was llvm-g++-4.2
MACOSX_DEPLOYMENT_TARGET='10.8' # was 10.5. C++11 is supported >=10.7
CXXFLAGS="$CXXFLAGS -std=gnu++11 -stdlib=libc++" # additional flags
Normal mex files without C++11 features are compiled well. Further, STL is well detected by the compiler except linking failure.
>> mex mextest.cpp
Undefined symbols for architecture x86_64:
"std::__1::__thread_struct::__thread_struct()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__thread_struct::~__thread_struct()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__thread_local_data()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__throw_system_error(int, char const*)", referenced from:
_mexFunction in mextest.o
"std::__1::thread::join()", referenced from:
_mexFunction in mextest.o
"std::__1::thread::~thread()", referenced from:
_mexFunction in mextest.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mex: link of ' "mextest.mexmaci64"' failed.
Error using mex (line 206)
Unable to complete successfully.
The actual source code is shown below. The details are not important because it compiles well in Matlab R2013 WINDOWS version with Visual Studio 2012 Express. An equivalent cpp was also well compiled with "clang++ -std=gnu++11 -stdlib=libc++ clangtest.cpp". So, at least, there is no logical error in the codes (I'm not saying it is safe codes. It is just a test.)
#include "mex.h"
#include <thread>
#include <stdio.h>
int count_thread1 = 0;
int count_thread2 = 0;
void hello()
{
count_thread2 = 0;
for(int i=0; i<=10000; i++){
for (int j=1;j<=20000;j++){
count_thread2 = i-j-1;
}
count_thread2++;
printf("2: %d , %d\n", count_thread1, count_thread2); // Not sure if printf is thread-safe in Matlab. But it works in this particular example
}
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
count_thread1 = 0;
std::thread t(hello);
for (int i=1;i<=10000;i++)
{
for (int j=1;j<=20000;j++){
count_thread1 = -i+j-1;
}
count_thread1++;
mexPrintf("1: %d , %d\n", count_thread1, count_thread2);
}
mexPrintf("\n");
t.join();
mexPrintf("Done\n");
}
It seems like I have to replace some include directories and/or library directories. What kind of options should be modify?
Thank you.
The error is due to compiling against -stdlib=libc++ but linking against -lstdc++. You can fix it in one of two ways:
Fix it in mexopts.sh. The most drastic and effective solution. Located in ~/.matlab/${MATLAB_VERSION}/mexopts.sh, this determines all compiler options. Simply find/replace all stdc++ to c++.
Patchwork solution: Simply add -lc++ to the tail end of CXXLIBS. I'm not sure what the effect of linking against multiple versions of the standard libraries is, but it seems to work. In your mex invocation, add the argument CXXLIBS="\$CXXLIBS -lc++".
As a secondary issue, I believe you're completely overwriting the value of CXXFLAGS; you must escape the $ symbol as I did above with the libraries.
Boost was installed and make by: brew install boost
Added the header path /usr/local/Cellar/boost/1.53.0/include to User Header Search Path
Added the library path /usr/local/Cellar/boost/1.53.0/lib to Library Search Path
main.cpp
#include <fstream>
#include <sstream>
#include <string>
#include "test.cpp"
void test(){
Test instance(true, 'm', 50, 17.89, "fuzhijie");
stringstream binary_sstream;
boost::archive::binary_oarchive binary_oa(binary_sstream);
binary_oa << instance;
}
int main(int argc, const char * argv[])
{
test();
return 0;
}
When I press CTRL+b, the following error message is shown:
Undefined symbols for architecture x86_64:
"boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::init()", referenced from:
boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::init(unsigned int) in main.o
"boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::save(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
void boost::archive::save_access::save_primitive<boost::archive::binary_oarchive, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(boost::archive::binary_oarchive&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.o
"boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::basic_binary_oprimitive(std::__1::basic_streambuf<char, std::__1::char_traits<char> >&, bool)", referenced from:
boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::binary_oarchive_impl(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, unsigned int) in main.o
"boost::archive::basic_binary_oprimitive<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::~basic_binary_oprimitive()", referenced from:
boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::~binary_oarchive_impl() in main.o
boost::archive::binary_oarchive_impl<boost::archive::binary_oarchive, char, std::__1::char_traits<char> >::binary_oarchive_impl(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, unsigned int) 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)
Does anyone have any suggestions on how to fix this?
The reason is maybe you have compiler gcc5 or gcc6.
And your code has used different compile flag with your boost library.
Detail please read this link
If you can recompile all incompatible libs you use, do it with compiler option
-D_GLIBCXX_USE_CXX11_ABI=1
and then rebuild your project. If still can not link, add change project's makefile compiler option to 0.
-D_GLIBCXX_USE_CXX11_ABI=0
======Update=======
The real solution should be install the new boost libraries (v1.62) for gcc6 or gcc5.
Error: " Undefined symbols for architecture x86_64:
"f(int, int, int const (*) [8], int const (*) [8], int*, int*)", referenced from:
_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)
I tried running the exact same code on Visual Studio 2010, and it worked! Any idea why it doesn't work here? My Mac is 64bit. Thanks!
Here's the code on the files that's giving the error:
#include <iostream>
using namespace std;
int p,q;
int f( int, int,const int [][8],const int [][8], int [],int []);
This happens if you haven't provided an implementation of your f(..) function.
In your main.cpp file, simply implement the function, like:
int f( int, int,const int [][8],const int [][8], int [],int [])
{
// Do stuff...
}
I have compiled boost 1.49.0 from source on a MacBook Pro running Mac OS X 10.7.3, I use Xcode 4.3.2 and Apple's LLVM 3.1 as my development environment. The following line of code (from boost http server1 example) results in a linking error as described:
void Connection::handleRead(const boost::system::error_code& error, size_t bytesTransfered) {
if (!error) {
boost::tribool result;
boost::tie(result, boost::tuples::ignore) = requestParser.parse(request, buffer.data(), buffer.data() + bytesTransfered);
if (result) {
requestHandler.handleRequest(request, reply);
async_write(socket, reply.toBuffers(), boost::bind(&Connection::handleWrite, shared_from_this(), placeholders::error));
}
else if (!result) {
reply = Reply::stockReply(Reply::badRequest);
async_write(socket, reply.toBuffers(), boost::bind(&Connection::handleWrite, shared_from_this(), placeholders::error));
}
else {
socket.async_read_some(boost::asio::buffer(buffer), boost::bind(&Connection::handleRead, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
}
}
else if (error != error::operation_aborted) {
connectionManager.stop(shared_from_this());
}
}
Undefined symbols for architecture x86_64:
"boost::tuples::tuple<boost::logic::tribool, char*, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> RequestParser::parse<char*>(Request&, char*, char*)", referenced from:
Connection::handleRead(boost::system::error_code const&, unsigned long) in Connection.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 searched the answers but I couldn't find anything related to it, neither I understand why the error is happening. The project links against lboost_system. Am I missing something? I am new to boost libraries.
It turns out my problem wasn't in this method, I mistakenly had RequestParser.parse() in the class implementation, checking back with the boost example the method is implemented in the declaration of the class.