boost::filesystem Undefined symbols for architecture x86_64 - c++

I am very new with using GNU. I am trying to start using the boost filesystem library, and I keep getting these errors. I am trying to get the current working directory, by using boost::filesystem.
My code:
boost::filesystem::path full_path( boost::filesystem::detail::current_path() );
cout << "Current path is : " << full_path << endl;
My command:
g++ -I boost_1_58_0 main.cpp -o example
Result:
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::current_path(boost::system::error_code*)", referenced from:
_main in main-1c56eb.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main-1c56eb.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main-1c56eb.o
___cxx_global_var_init1 in main-1c56eb.o
ld: symbol(s) not found for architecture x86_64
Can someone please explain what the error is asking for? What did I do wrong?

boost.filesystem is not a header-only library. You have to link to the library using -L and -l flags. (And make sure the library is already properly built). You need to link to both boost_system and boost_filesystem libraries.
The command line could look like:
g++ -Iboost_1_58_0 -Lboost_1_58_0/lib/ -lboost-filesystem -lboost_system main.cpp -o example
(replace the -L argument with the path where the libboost-filesystem.so file resides)
Then, before you are able run the executable, you have to let the loader know where to look for the libraries. You shell be able do that using the following command:
export DYLD_LIBRARY_PATH=/path/to/boost/bib:$DYLD_LIBRARY_PATH
To make it automatic, I would recommend using a build system like cmake instead of just a command line.

Related

PostgreSQL external C function link failed on Mac OSX

I'm trying to build an external PostgreSQL function on OSX 10.11 with both clang and gcc, but link failed with the following errors:
c++ -I/usr/local/Cellar/postgresql/9.5.3/include/server -fpic -c ./main.c
c++ -shared -o ttt.dylib main.o
Undefined symbols for architecture x86_64:
"_deconstruct_array", referenced from:
_psql_nearest in main.o
"_elog_finish", referenced from:
_psql_nearest in main.o
"_elog_start", referenced from:
_psql_nearest in main.o
"_get_typlenbyvalalign", referenced from:
_psql_nearest in main.o
"_pfree", referenced from:
_psql_nearest in main.o
"_pg_detoast_datum", referenced from:
_psql_nearest 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)
It looks like I need to link my library with some of PostgreSQL libraries. What are these libraries?
main.cpp:
extern "C" {
#include <postgres.h>
#include <fmgr.h>
#include <utils/array.h>
#include <utils/lsyscache.h>
#include <catalog/pg_type.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(psql_nearest);
Datum psql_nearest(PG_FUNCTION_ARGS) {
if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
elog(ERROR, "DOC2VEC: NULL INPUT DATA");
PG_RETURN_NULL();
}
ArrayType *_docVector = PG_GETARG_ARRAYTYPE_P(0);
Oid elTypeVals = ARR_ELEMTYPE(_docVector);
if (elTypeVals != FLOAT4OID) {
elog(ERROR, "DOC2VEC: INVALID INPUT DATA TYPE");
PG_RETURN_NULL();
}
int16 typeLenVals = 0;
bool typeByValVals = false;
char typeAlignVals = char(0);
get_typlenbyvalalign(elTypeVals, &typeLenVals, &typeByValVals, &typeAlignVals);
Datum *inputVals;
bool *nullVals;
int nVals;
deconstruct_array(_docVector, elTypeVals, typeLenVals, typeByValVals, typeAlignVals, &inputVals, &nullVals, &nVals);
pfree(inputVals);
pfree(nullVals);
PG_RETURN_NULL();
}
}
Thanks to PostgreSQL developers, they explained me the difference in Linux and OSX linking of external functions.
Instead of -shared you need -bundle -bundle_loader /path/to/postgres,
and there are some other linker flags that are advisable too.
Also, PostgreSQL expects the file extension for loadable modules to be .so even on OSX.
It's usually better to use PGXS to build extensions, instead of
learning such details for yourself.
Or you can crib from one of the extensions in the contrib/
source tree.
If you need to link, you will need the -L flag to point the linker to the path where the postgres libraries are located (the linker equivalent of the -I compiler flag). and the -l flag to actually link the libraries (one for each library); the library name without the lib prefix and without the extension.
In your case, something along the lines of -L/usr/local/Cellar/postgresql/9.5.3/lib -lpostgres
(There's a variety of library files in that directory; try -lpg to start with.
The reference to _pfree in your error message also suggest to link pgcommon, which contains the implementation of pgree (at least when using nm libpgcommon.a).
)
You may want to read up a bit more on compiling and linking in general; you do the right thing for compiling with the -I flag, but oddly then miss out on the linking step. And learning about make and Makefiles will come in handy.
I also don't understand the extern "C" { part for a .c file, which is clearly a C-only file. extern "C" is usually used in C++ files for compatibility with C.

ld: symbol(s) not found for architecture

First of all I want to say I realise this has been asked a dozen times, and I've read countless solutions without any luck so therefore I'm asking again.
I'm using OS X Mavericks 10.9.5, writing in Sublime 3 and compiling using terminal with g++
I'm trying to compile this simple file (test.cpp)
#include <iostream>
#include <SDL2/SDL.h>
int main () {
if(SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "I made it this far" << std::endl;
}
return 0;
}
Compiler line :
g++ test.cpp
This returns the error :
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-ebbae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, I've tried adding a lot of different flags, -m32 only changes the result to throw Undefined symbols for architecture i386: and ld: symbol(s) not found for architecture i386
I've tried different -arch variations, but I can't seem to get it to work. also played around with -l -I flags but I'm not sure I know what they do/how they could help..
Does anyone have a clue what to do next?
EDIT : Some additional information. I've tried using the .framework for SDL2, that didn't change anything, currently I'm using SDL2 installed through compilation of the source. Headers located in usr/local/SDL2
g++ test.cpp
You should specify the SDL library, too:
g++ test.cpp -lsdl2
You might need to include a path if its not well known to the compiler driver:
g++ test.cpp -L/path/to/the/library -lsdl2

Trouble setting up Lua in Xcode

How can I include Lua in my project in Xcode?
I have installed Lua via the instructions on the website (curl, extract, make macosx install, etc).
I can reference lua
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
But when I go to use it, I get an error (even writing something as simple as the following)
lua_State *L = luaL_newstate();
lua_close(L);
It tells me :
Undefined symbols for architecture x86_64:
"_luaL_newstate", referenced from:
_main in main.o
"_lua_close", 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)
Any advice would be much appreciated. If you need further information, let me know and I can edit it in. Thanks!
EDIT:
Added the -llua flag (SFML included the /usr/local/lib and include). Now getting "ignoring file /usr/local/lib/liblua.a, file was built for archive which is not the architecture being linked (i386): /usr/local/lib/liblua.a"
EDIT2:
I changed Base SDK to Latest OS X (OS X 10.9) and Build Active Architecture Only to "Yes" and now it will compile.
In Xcode, select < ProjectName > with blue icon on top of the left pane (where all the sources are), then in main window select a target under Targets. In Build Settings tab, select All instead of Basic and set following parameters:
Other Linker Flags = -llua
Header Search Paths = /usr/local/include
Library Search Paths = /usr/local/lib
Assuming Lua headers were installed in /usr/local/include, and liblua.a in /usr/local/lib.
You may also use search field to find them.

Linker Flag/Boost not found -- linking Boost filesystem with Xcode

I am trying to the use Boost Filesystem library in Xcode.
I installed Boost with brew install boost, and it was successful.
Turns out Boost wasn't linked so I ran brew link --overwrite Boost, which worked.
I am trying to #include <boost/filesystem.hpp> in my project, but have not been successful so far.
At first Xcode couldn't find <boost/filesystem.hpp>, so I added
/usr/local/lib
/usr/local/include
to my Header search paths for Target and Project.
That fixed the original problem, but then boost_system could not be found by Xcode.
Here is the transcript:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.o
___cxx_global_var_init1 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 added the -lboost_system linker flag to my project, and now I am getting this error:
ld: library not found for -lboost_system
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And it's definitely the correct linker flag, so perhaps Boost installed incorrectly after all.
I will update this question if I find a solution, but I would appreciate help in solving this problem.
You only need /usr/local/include in your header search path and you need /usr/local/lib in your library search path in order for the linker to find the libraries.
Had the same problem in xcode 6 . user header search path and library search was okay . still showed link error . I solved the problem by adding -lboost_system-mt in other linker flag in project target setting

Boost Tcp-Asio-Server Link failure using Qt on Mac

I have a very simple class, which I will send as Object via TCP using Asio by boost. I found many examples on the Internet, but when I compile my code I get a Link Failure.
#include <boost/archive/text_oarchive.hpp>
void async_write(){
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream); // here it fails
//....
}
I downloaded boost via macports.
My Qt project file:
INCLUDEPATH += /opt/local/include
LIBS += -L/opt/local/lib
LIBS += -lboost_system-mt -lboost_filesystem-mt -lboost_serialization-mt
Failure:
Undefined symbols for architecture x86_64:
"boost::archive::text_oarchive_impl::save(std::string const&)", referenced from:
void boost::archive::save_access::save_primitive(boost::archive::text_oarchive&, std::string const&) in tcpsession.o
"boost::archive::text_oarchive_impl::text_oarchive_impl(std::ostream&, unsigned int)", referenced from:
boost::archive::text_oarchive::text_oarchive(std::ostream&, unsigned int) in tcpsession.o
"boost::archive::basic_text_oprimitive::~basic_text_oprimitive()", referenced from:
boost::archive::text_oarchive_impl::~text_oarchive_impl() in tcpsession.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: * [tcpserver] Error 1
Any help is appreciated.
The standard reply would be "re-run qmake". It's even simpler to just delete the entire shadow build directory. One level up from the source of your project, there will be build-... folders. Delete all of them, and build again.
The simplest way to see whether your project file (the file with LIBS += lines) is current, add something to it that's invalid (like a typo in the library name). If it still "builds" without showing the error directly aimed at your typo (a missing library), then you know that your build is not based on the current project file, and you should re-run qmake or delete the shadow build folder so that everything will be recreated there.