I tried to compile the following code on ubuntu 11.04:
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
int main()
{
boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, "Highscore", boost::interprocess::read_write);
shdmem.truncate(1024);
std::cout << shdmem.get_name() << std::endl;
boost::interprocess::offset_t size;
if (shdmem.get_size(size))
std::cout << size << std::endl;
}
only to get the following errors:
/tmp/cc786obC.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)':
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0xe0): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x116): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x16c): undefined reference to `shm_open'
shared_memory.cpp:(.text._ZN5boost12interprocess20shared_memory_object19priv_open_or_createENS0_6detail13create_enum_tEPKcNS0_6mode_tERKNS0_11permissionsE[boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::detail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)]+0x1c0): undefined reference to `shm_open'
collect2: ld returned 1 exit status
Command I used to compile the file: g++ -o shared shared.cpp
Command I used to install the boost libraries: sudo apt-get install libboost-dev libboost-doc
shm_open is made available by linking librt. Try passing -lrt flag to the linker.
Try:
g++ -c -Wall shared.cpp
g++ -L /lib -lrt shared.o -o shared
Just adding to #anio's answer:
While linking, the -lrt flag may need to be added at the end of the command.
Try:
g++ -L /lib shared.o -o shared -lrt
My same problem got solved from #anio's answer but I needed to do additional work. As I cannot comment due to low reputation. So I am presenting my pennies, may be someone find it helpful. I am baby stepping everything, so "sorry" if I seem childish.
I am using Eclipse on Debian for cross compiling for arm-linux-gnueabihf-g++. So I first found the location for "librt"
/$ find -iname "librt*"
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.a
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librt.so
./home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf/librtmp.so.0
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt-2.13.so
./home/myuser/targetsysroot/lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt.so.1
./lib/arm-linux-gnueabihf/librt-2.19.so
./lib/i386-linux-gnu/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt.so.1
./lib/i386-linux-gnu/i686/cmov/librt-2.19.so
./lib/i386-linux-gnu/librt-2.19.so
As I prefer to sync with the remote target machine I have added "sysroot path" for my library into eclipse project properties "Library Search Path (-L)"
/home/myuser/targetsysroot/usr/lib/arm-linux-gnueabihf
Also added "rt" to Libraries (-l), which ultimately solved my problem.
In case you are compiling with the use
g++ -L $YOUR_PATH_TO_LIB$ shared.o -o shared -lrt
replace $YOUR_PATH_TO_LIB with yours.
g++ -L /lib shared.o -o shared -lrt -lpthread
Related
I always have problems when building libraries and linking them, so I hope someone can give me a hand.
I downloaded libtorrent from here and I've built it like they explain here in the building with autotools section (skipping step 1). The building process was successfull I think, but when I did make check the output was:
============================================================================
Testsuite summary for libtorrent-rasterbar 1.0.5
============================================================================
# TOTAL: 0
# PASS: 0
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
Maybe it should say: total X?
I did a little program where I added #include <libtorrent/session.hpp> and when I compile with g++ file.cpp -o file it says libtorrent/session.hpp: No such file or directory.
Should I add some flags to g++ such as -lpthread for other projects and thinks like that?
Thanks
UPDATE:
When installing without building using sudo apt-get install libtorrent-rasterbar-dev and compiling my main.cpp file I get this error:
g++ main.cpp -o file
In file included from /usr/include/libtorrent/session.hpp:49:0,
from main.cpp:2:
/usr/include/libtorrent/config.hpp:46:2: error: #error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in order for asio's declarations to be correct. If you're linking dynamically against libtorrent, define BOOST_ASIO_DYN_LINK otherwise BOOST_ASIO_SEPARATE_COMPILATION. You can also use pkg-config or boost build, to automatically apply these defines
#error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in \
UPDATE 2:
Modified the main.cpp file to add the the following above the libtorrent #include directives:
#ifndef BOOST_ASIO_DYN_LINK
#define BOOST_ASIO_DYN_LINK
#endif
But then I have this problem:
$ g++ main.cpp
/tmp/ccM2ItFb.o: In function `main':
main.cpp:(.text+0x57): undefined reference to `libtorrent::default_storage_constructor(libtorrent::file_storage const&, libtorrent::file_storage const*, std::string const&, libtorrent::file_pool&, std::vector<unsigned char, std::allocator<unsigned char> > const&)'
main.cpp:(.text+0xb9): undefined reference to `libtorrent::session::~session()'
main.cpp:(.text+0x105): undefined reference to `libtorrent::session::~session()'
/tmp/ccM2ItFb.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x162): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x16e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x17a): undefined reference to `boost::system::system_category()'
main.cpp:(.text+0x192): undefined reference to `boost::asio::error::get_netdb_category()'
main.cpp:(.text+0x19e): undefined reference to `boost::asio::error::get_addrinfo_category()'
main.cpp:(.text+0x1aa): undefined reference to `boost::asio::error::get_misc_category()'
/tmp/ccM2ItFb.o: In function `boost::asio::error::get_system_category()':
main.cpp (.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
/tmp/ccM2ItFb.o: In function `libtorrent::session::session(libtorrent::fingerprint const&, int, unsigned int)':
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x3c): undefined reference to `libtorrent::rel_clocktime_pools_nolog_resolvecountries_deprecated_dht_ext_()'
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x75): undefined reference to `libtorrent::session::init(std::pair<int, int>, char const*, libtorrent::fingerprint const&, int, unsigned int)'
/tmp/ccM2ItFb.o: In function `void boost::checked_delete<libtorrent::torrent_info const>(libtorrent::torrent_info const*)':
main.cpp:(.text._ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_[_ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_]+0x1a): undefined reference to `libtorrent::torrent_info::~torrent_info()'
collect2: error: ld returned 1 exit status
Also tried compiling using: g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp with the same result.
UPDATE 3:
I was able to solve the problem. I had to compile using:
g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp and add also the link library -ltorrent-rasterbar
Sounds like you've compiled the library but not actually installed it anywhere. An #include directive looks in /usr/include by default, but the libtorrent headers are somewhere in the directory where you built the library. You'll need to either install the library's files into the system directories, or give the compiler an -I option pointing to the libtorrent build directory. (You'll probably also need an -L option when linking, for the same reason.)
I have downloaded the latest Poco library poco-1.7.3.tar. Configured with few properties and did make install.
Tried the following sample helloworld program.
#include <iostream>
#include <Poco/Util/Application.h>
class HelloPocoApplication : public Poco::Util::Application
{
protected:
virtual int main(const std::vector<std::string> &args)
{
std::cout << "Hello, POCO C++ Libraries!" << std::endl;
return EXIT_OK;
}
};
POCO_APP_MAIN(HelloPocoApplication);
Compiled it using
g++ -I poco-1.7.3/Util/include -I poco-1.7.3/XML/include -I poco-1.7.3/JSON/include -I poco-1.7.3/Foundation/include -L poco-1.7.3/lib/Linux/x86_64 -lPocoUtil -lPocoFoundation -lPocoXML -lPocoJSON helloworld.cpp -o prog
But it is throwing following errors
/tmp/ccFvl4ll.o: In function `main':
helloworld.cpp:(.text+0x4f): undefined reference to `Poco::Util::Application::init(int, char**)'
helloworld.cpp:(.text+0xd9): undefined reference to `Poco::Logger::log(Poco::Exception const&)'
/tmp/ccFvl4ll.o: In function `Poco::RefCountedObject::release() const':
helloworld.cpp:(.text._ZNK4Poco16RefCountedObject7releaseEv[_ZNK4Poco16RefCountedObject7releaseEv]+0x6e): undefined reference to `Poco::Bugcheck::unexpected(char const*, int)'
/tmp/ccFvl4ll.o: In function `Poco::Util::Application::logger() const':
helloworld.cpp:(.text._ZNK4Poco4Util11Application6loggerEv[_ZNK4Poco4Util11Application6loggerEv]+0x2c): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
/tmp/ccFvl4ll.o: In function `HelloPocoApplication::HelloPocoApplication()':
helloworld.cpp:(.text._ZN20HelloPocoApplicationC2Ev[_ZN20HelloPocoApplicationC5Ev]+0x14): undefined reference to `Poco::Util::Application::Application()'
/tmp/ccFvl4ll.o:(.gcc_except_table+0x2c): undefined reference to `typeinfo for Poco::Exception'
/tmp/ccFvl4ll.o: In function `Poco::AutoPtr<HelloPocoApplication>::operator->()':
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x3a): undefined reference to `Poco::NullPointerException::NullPointerException(int)'
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x3f): undefined reference to `Poco::NullPointerException::~NullPointerException()'
helloworld.cpp:(.text._ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv[_ZN4Poco7AutoPtrI20HelloPocoApplicationEptEv]+0x44): undefined reference to `typeinfo for Poco::NullPointerException'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x20): undefined reference to `Poco::Util::Application::name() const'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x28): undefined reference to `Poco::Util::Application::initialize(Poco::Util::Application&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x30): undefined reference to `Poco::Util::Application::uninitialize()'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x38): undefined reference to `Poco::Util::Application::reinitialize(Poco::Util::Application&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x40): undefined reference to `Poco::Util::Application::defineOptions(Poco::Util::OptionSet&)'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x48): undefined reference to `Poco::Util::Application::run()'
/tmp/ccFvl4ll.o:(.rodata._ZTV20HelloPocoApplication[_ZTV20HelloPocoApplication]+0x50): undefined reference to `Poco::Util::Application::handleOption(std::string const&, std::string const&)'
/tmp/ccFvl4ll.o: In function `HelloPocoApplication::~HelloPocoApplication()':
helloworld.cpp:(.text._ZN20HelloPocoApplicationD2Ev[_ZN20HelloPocoApplicationD5Ev]+0x1f): undefined reference to `Poco::Util::Application::~Application()'
/tmp/ccFvl4ll.o:(.rodata._ZTI20HelloPocoApplication[_ZTI20HelloPocoApplication]+0x10): undefined reference to `typeinfo for Poco::Util::Application'
collect2: error: ld returned 1 exit status
Can you please help me?
I managed to run exactly that code but using CMake rather than Makefile and it worked. you have just to change the paths according to your machine.
Here's my CMakeLists.txt :
cmake_minimum_required(VERSION 2.8.3)
project(tutocpp14)
set(Poco_DIR "/usr/local/lib/cmake/Poco/")
set(Poco_INCLUDE_DIRS "/usr/include/Poco/")
find_package(Poco REQUIRED COMPONENTS Foundation Net XML Util) # add other components here
# check c++11 / c++0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11 " COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "-std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "-std=c++0x")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
include_directories( ${Poco_INCLUDE_DIRS})
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${Poco_LIBRARIES})
Cheers,
Checking with Poco documents it seems it should work. Looking at an example here, and its makefile, I suspect the order of your library is reversed. Assuming that paths are correct (since ld didn't report on that) I would say that linking order is incorrect. See here Why.
So I suggest you change your command to
g++ -I poco-1.7.3/Util/include -I poco-1.7.3/Foundation/include -L poco-1.7.3/lib/Linux/x86_64 -lPocoFoundation -lPocoUtil helloworld.cpp -o prog
in case those examples goes out of scope somehow in future, here is the code and makefile copied.
#include <iostream>
#include <Poco/Util/Application.h>
class HelloPocoApplication : public Poco::Util::Application
{
protected:
virtual int main(const std::vector<std::string> &args)
{
std::cout << "Hello, POCO C++ Libraries!" << std::endl;
return EXIT_OK;
}
};
POCO_APP_MAIN(HelloPocoApplication);
Note: Remember to restore the tabs instead of spaces in makefile code!
POCO_LIBS=-lPocoFoundation -lPocoUtil
all: hello-poco
clean:
rm -f hello-poco
hello-poco: hello-poco.cpp
$(CXX) $(POCO_LIBS) -o hello-poco hello-poco.cpp
Update:
With all above said, I have to put a note on strangeness of your error especially Poco::Logger which is found in foundation library. So it seems that library does not get linked. I would suggest two things:
1. Check if its not corrupted. You can do that by cleaning and recompiling
2. Try to install in standard path so that you avoid linking paths
I know all these are not definitive (guess works at best) but will be useful to provide you hints of where the issue lies.
Your original library linking order was fine, but you are missing XML and JSON (Util depends on these libraries). Although, the link errors you've got indicate that you may be either missing Foundation library or mixing library versions.
$ echo $POCO_BASE
/tmp/poco
$ pwd
/tmp/poco/test
$ g++ -I $POCO_BASE/Util/include -I $POCO_BASE/Foundation/include -L $POCO_BASE/lib/Linux/x86_64 -lPocoUtil -lPocoXML -lPocoJSON -lPocoFoundation helloworld.cpp -o prog
try this command to compile.
g++ <src_file>.cpp -lPocoFoundation -lPocoJSON -lPocoUtil -lPocoJWT
-o <binary_name>
my gcc/g++ version is 4.1.2 , ACE-6.10 in CentOS 5.10 and I make the ACE Library with static_libs=1 option to get the static library, after make and make install ,I get such libraries such as libACE.so , libACE.a and so on, and then I write following code to test, The codes shows as follow:
#include <ace/Log_Msg.h>
#include <ace/OS_main.h>
using namespace std;
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
ACE_DEBUG( (LM_DEBUG, ACE_TEXT("Hello World!\n") ) );
return 0;
}
then I use the following two method to compile and link :
method 1:
g++ -p -o acetest acetest.cpp /usr/local/src/ACE_wrappers/lib/libACE.a -I$ACE_ROOT -I$ACE_ROOT/ace -pthread -ldl -lrt
method 2:
[root#localhost testCode]# g++ -p -o acetest acetest.cpp -L/usr/local/src/ACE_wrappers/lib -lACE -I$ACE_ROOT -I$ACE_ROOT/ace -pthread -ldl -lrt
/tmp/cc0eKwlC.o: In function `main':
acetest.cpp:(.text+0x15): undefined reference to `ACE_Log_Msg::last_error_adapter()'
acetest.cpp:(.text+0x1d): undefined reference to `ACE_Log_Msg::instance()'
acetest.cpp:(.text+0x3f): undefined reference to `ACE_Log_Msg::conditional_set(char const*, int, int, int)'
acetest.cpp:(.text+0x57): undefined reference to `ACE_Log_Msg::log(ACE_Log_Priority, char const*, ...)'
collect2: ld 返回 1
And it comes to the question, the method 1 to use static library is corrent, why method 2,which uses the dynamic library, is wrong?
hunger for the answer, thanks all;
When you compile your application you should add the -DACE_AS_STATIC_LIBS flag to the compiler to indicate that you want to link statically with ACE
Try adding no_hidden_visibility=1 to your platform_macros.GNU file. I believe ACE builds its shared libraries with symbols hidden by default.
See here for the benefits this can provide. However, it doesn't seem to work well when mixing static and dynamic libraries. If anyone has more info on why that is, feel free to chime in.
I'm trying to compile a very simple program (sample that does nothing) with pqxx, but cannot do it.
Here's the 'program':
$ cat xx.cpp
#include <pqxx/pqxx>
using namespace pqxx;
int main()
{
connection conn("dbname=dout1");
return 0;
}
The command I used to try to compile in C++:
g++ -I /usr/local/include/ -L /usr/local/lib/ -l pqxx -I /opt/postgres_home/include/ -L /opt/postgres_home/lib/ -lpq xx.cpp
Message returned:
/tmp/cc18wMEy.o: In function `pqxx::connect_direct::connect_direct(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
xx.cpp:(.text._ZN4pqxx14connect_directC1ERKSs[pqxx::connect_direct::connect_direct(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x1f): undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
xx.cpp:(.text._ZN4pqxx14connect_directC1ERKSs[pqxx::connect_direct::connect_direct(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x2a): undefined reference to `vtable for pqxx::connect_direct'
/tmp/cc18wMEy.o: In function `pqxx::connect_direct::~connect_direct()':
xx.cpp:(.text._ZN4pqxx14connect_directD1Ev[pqxx::connect_direct::~connect_direct()]+0x13): undefined reference to `vtable for pqxx::connect_direct'
xx.cpp:(.text._ZN4pqxx14connect_directD1Ev[pqxx::connect_direct::~connect_direct()]+0x1f): undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'
/tmp/cc18wMEy.o: In function `pqxx::basic_connection<pqxx::connect_direct>::basic_connection(char const*)':
xx.cpp:(.text._ZN4pqxx16basic_connectionINS_14connect_directEEC1EPKc[pqxx::basic_connection<pqxx::connect_direct>::basic_connection(char const*)]+0x2b): undefined reference to `pqxx::connection_base::connection_base(pqxx::connectionpolicy&)'
xx.cpp:(.text._ZN4pqxx16basic_connectionINS_14connect_directEEC1EPKc[pqxx::basic_connection<pqxx::connect_direct>::basic_connection(char const*)]+0xd6): undefined reference to `pqxx::connection_base::init()'
/tmp/cc18wMEy.o: In function `pqxx::basic_connection<pqxx::connect_direct>::~basic_connection()':
xx.cpp:(.text._ZN4pqxx16basic_connectionINS_14connect_directEED1Ev[pqxx::basic_connection<pqxx::connect_direct>::~basic_connection()]+0x17): undefined reference to `pqxx::connection_base::close()'
collect2: ld returned 1 exit status
If I do like:
$ g++ -I /usr/local/include/pqxx -L /usr/local/lib/ -l pqxx -l pq xx.cpp
Returns:
/usr/lib64/gcc/x86_64-slackware-linux/4.4.4/../../../../x86_64-slackware-linux/bin/ld: cannot find -lpq
collect2: ld returned 1 exit status
Considering:
$ cat /etc/ld.so.conf
/usr/local/lib
/usr/local/pgsql/lib <-- just symlink to /opt/postgres_home/
/opt/postgres_home/lib
/usr/x86_64-slackware-linux/lib
/usr/lib64/seamonkey
/opt/kde3/lib64
/usr/lib64/jre1.6.0_16/lib/amd64
I really don't know what else to do, because:
# ls /opt/postgres_home/lib/libpq*
/opt/postgres_home/lib/libpq.a /opt/postgres_home/lib/libpq.so.5.3
/opt/postgres_home/lib/libpq.so /opt/postgres_home/lib/libpqwalreceiver.so
/opt/postgres_home/lib/libpq.so.5
Luis
After an email exchange with pqxx author Jeroen T. Vermeulen, I could manage to compile the 'program'.
The idea was to place xx.cpp before -lpqxx -lpq
So, the entire command is:
g++ -I /usr/local/include/pqxx/ -L /usr/local/lib/ -I /usr/local/pgsql/include/ -L /usr/local/pgsql/lib/ xx.cpp -lpqxx -lpq
It works now.
Works for me here with a simple g++ -lpqxx xx.cpp, so it terribly looks like a broken installation. Note that you don't need to link with libpq, as libpqxx with take care of that for you if you are using shared objects.
Check that you don't have the libpqxx library installed in different places, like /usr/lib, /usr/local, and /usr/local/pgsql/lib.
So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.
In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.