Can't compile code with CVC4 C++ API - c++

I am just trying to compile this file helloworld.cpp
#include <iostream>
#include <cvc4/cvc4.h>
using namespace CVC4;
int main() {
ExprManager em;
Expr helloworld = em.mkVar("Hello World!", em.booleanType());
SmtEngine smt(&em);
std::cout << helloworld << " is " << smt.query(helloworld) << std::endl;
return 0;
}
using g++ helloworld.cpp -lcvc4 -o helloworld -lcvc4 -Wno-deprecated. But it is giving me this error
/tmp/cc9SFpL4.o: In function `main':
helloworld.cpp:(.text+0xac): undefined reference to `CVC4::ExprManager::mkVar(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, CVC4::Type, unsigned int)'
collect2: error: ld returned 1 exit status
Help!
I have installed CVC4 adding repo link in /etc/apt/sources.list and then calling sudo apt-get install cvc4 libcvc4-dev libcvc4parser-dev.
EDIT: I mistyped g++ helloworld.cpp -lcvc4 ... I used g++ helloworld.cpp -o helloworld -lcvc4 -Wno-deprecated. Actually I used all combinations, permutations.

This seems to be a problem with the OP's environment. Both r4C9rAyrd6A1 and I were able to compile the example on our local machines. The specific issue might have been that the OP's compiler wanted the -lcvc4 linker flag after the other flags, e.g. g++ helloworld.cpp -Wno-deprecated -o helloworld -lcvc4 as mentioned in the comments.

Related

C++ Boost Example: Creating and Managing Threads (Compilation Error)

I am currently using Boost 1.54.0. I am following the code from this example.
example_44_01.cpp
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}
void thread()
{
for (int i = 0; i < 5; ++i)
{
wait(1);
std::cout << i << std::endl;
}
}
int main(int argc, char** argv)
{
boost::thread t{thread};
t.join();
return 0;
}
So, it looks like all I need is the -lboost_thread, and -lboost_chrono libraries to link to at compile time. I also added the -lboost_system.
Here are my execution scripts.
g++-7 -Wall -std=c++1z -g -c example_44_01.cpp -o example_44_01.o
g++-7 -Wall -std=c++1z -g example_44_01.o -o example_44_01 -lboost_system -lboost_thread -lboost_chrono &>result.txt
What's going on here? This is the result.txt file:
example_44_01.o: In function `boost::this_thread::sleep_for(boost::chrono::duration<long, boost::ratio<1l, 1000000000l> > const&)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:243: undefined reference to `boost::this_thread::hidden::sleep_for(timespec const&)'
collect2: error: ld returned 1 exit status
I've compiled and linked other programs with the same libraries without error. So is the error in the code? This seems doubtful as the code is straight from the documentation. Any insight is appreciated.
I had this issue once because I was indeliberately using different versions of Boost (I had first installed Boost from commandline, then a few months later on, manually from zip).
Try adding the path to your Boost libraries to the compiler. For instance, if your libraries are stored at /usr/local/lib, try:
g++-7 -Wall -std=c++1z -g example_44_01.o -o example_44_01 -L/usr/local/lib -lboost_system -lboost_thread -lboost_chrono &>result.txt

C++ BOOST undefined reference to `boost::filesystem::detail::copy_file

I have no clue why boost::filesystem::copy_file is making trouble for me.
undefined reference to `boost::filesystem::detail::copy_file
// g++ -std=c++11 test.cpp -lboost_filesystem -lboost_system -lrt -lboost_wave
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::create_directory("aaa");
// ok
boost::filesystem::copy_file("f1","f2");
// /tmp/ccNWZltB.o: In function `boost::filesystem::copy_file(boost::filesystem::path const&, boost::filesystem::path const&)':
// test.cpp:(.text._ZN5boost10filesystem9copy_fileERKNS0_4pathES3_[_ZN5boost10filesystem9copy_fileERKNS0_4pathES3_]+0x26): undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
// collect2: error: ld returned 1 exit status
return 0;
}
I got no inspiration from the source code of boost or its help:
inline
void copy_file(const path& from, const path& to, // See ticket #2925
BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
{detail::copy_file(from, to, option, &ec);}
Even such a simple example does not work for me.
Platform: Linux Ubuntu 64
There is a workaround for this problem, replace
#include <boost/filesystem.hpp>
with
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
Or, preferably, add -DBOOST_NO_CXX11_SCOPED_ENUMS to your compiler flags
If you run into this problem make sure to include both -lboost_system and -lboost_filesystem in your call to g++
Example working Makefile
BINARY = output
FILE_OBJECTS = main.o fileLoader.o
BOOST = -lboost_system -lboost_filesystem
GCC = g++ -std=c++17
FLAGS = -Wall -pedantic -Wextra
build: $(FILE_OBJECTS)
$(GCC) $(FLAGS) $(FILE_OBJECTS) -o $(BINARY) $(BOOST)
main.o: main.cpp fileLoader.o
$(GCC) $(FLAGS) -c main.cpp
fileLoader.o: fileLoader.cpp
$(GCC) $(FLAGS) -c fileLoader.cpp
clean:
rm -rf *.o $(BINARY)
Example working code
#include <boost/filesystem.hpp>
void create_data_file(std::string file_path)
{
boost::filesystem::path p(file_path);
boost::filesystem::create_directory(p);
}
I could not compile a file that included the header boost/filesystem.hpp either. This is how I solved it: I commented out the line boost/filesystem.hpp and all the lines that were using Boost, and then compiled the file. I then uncommented all the lines in the files and compiled again, and then it worked. I was compiling with the flag -lboost_system both times!
In older boost versions it is BOOST_NO_SCOPED_ENUMS, not BOOST_NO_CXX11_SCOPED_ENUMS
see boost::filesystem::copy_file() missing symbol in c++11

undefined reference to `function_name'

I moved from Windows to Ubuntu and I wanted to try some C++ programming on Ubuntu. So here is very simple code and very stupid error which I can't resolve:
horse.h
#ifndef _horse_
#define _horse_
class Horse{
int speed;
public:
void saySomething();
};
#endif
horse.cpp
#include "horse.h"
#include <iostream>
using namespace std;
void Horse::saySomething(){
cout << "iiiihaaaaaaa brrrrr."<<endl;
}
and Main.cpp
#include "horse.h"
int main(){
Horse h;
h.saySomething();
}
After I compile (compilation is successful) and run this I get this error message:
/tmp/ccxuDyrd.o: In function `main':
Main.cpp:(.text+0x11): undefined reference to `Horse::saySomething()'
collect2: ld returned 1 exit status
Please help me somehow.
Try
g++ -c main.cpp horse.cpp (to compile)
g++ -o a.out main.o horse.o (to link)
It seems you only compiled your code but did not link the resulting object files. You probably invoked the compiler like this:
g++ main.cpp
You should instead compile every *.cpp file separately and then link each resulting *.o file. And you should do this with a Makefile.
Actually, the basic idea is the same on Windows with MSVC. The compiler produces object files, the linker links them together.

undefined reference to xercesc_2_8 when compiling sample ocde

I'm trying to compile the following sample code available at XERCES site:
#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.
XERCES_CPP_NAMESPACE_USE
int main(int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
with,
g++ -g -Wall -pedantic -L/usr/lib -lxerces-c -o xercesTest xercesTest.cpp
giving me the following linking error:
/tmp/ccYIHCfR.o: In function `main':
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLUni::fgXercescDefaultLocale'
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_2_8::PanicHandler*, xercesc_2_8::MemoryManager*, bool)'
/home/cjmv/temp/xercesTest.cpp:18: undefined reference to `xercesc_2_8::XMLPlatformUtils::Terminate()'
/tmp/ccYIHCfR.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_2_8::XMLException'
collect2: ld returned 1 exit status
I've installed xerces-c28 and xerces-c2-dev through aptitude on my ubuntu-server 12.04
Any help would be appreciated.
Put the library last on the command line:
g++ -g -Wall -pedantic -L/usr/lib -o xercesTest xercesTest.cpp -lxerces-c
include the lib path of xerces:
try this
g++ -I/<xerces-c 2.8.0 path>/include -c xercesTest.cpp
g++ -L/<xerces-c 2.8.0 path>/lib -lxerces-c xercesTest.o

Problems with wrapping a C++ code

To try the C++ code wrapping within C, I used the following:
header.h
#ifdef __cplusplus
extern "C"
#endif
void func();
source.cpp
#include "header.h"
#include <iostream>
extern "C" void func()
{
std::cout << "This is C++ code!" << std::endl;
}
and source.c
#include "header.h"
int main()
{
func();
}
To compile and link, I used the following sequence:
g++ -c source.cpp
gcc source.c source.o -o myprog
The error I get is:
ence to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
source.cpp:(.text+0x1c): undefined reference tostd::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
source.o: In function __static_initialization_and_destruction_0(int, int)':
source.cpp:(.text+0x45): undefined reference tostd::ios_base::Init::Init()'
source.cpp:(.text+0x4a): undefined reference to std::ios_base::Init::~Init()'
source.o:(.eh_frame+0x12): undefined reference to__gxx_personality_v0'
collect2: ld returned 1 exit status
How can I make this simple code compile and run? It should serve as a basis for my future
development.
Link with g++ as well:
g++ -c source.cpp
g++ source.c source.o -o myprog
Or better:
g++ -c source.cpp -o source_cpp.o
gcc -c source.c -o source_c.o
g++ -o myprog source_cpp.o source_c.o
Best to avoid the common prefix source.{cpp,c} as it causes confusion.
You'll still have to link with the C++ linker:
gcc -o source-c.o source.c
g++ -o source-p.o source.cpp
g++ -o myprog source-c.o source-p.o
Your C++ object file will need to resolve symbols from the C++ library, which only the C++ linker will pull in automatically. (Alternatively, you could specify the library manually for the C linker.)
you can't do that, you can compile C code with gcc, and then link it with c++ objects with g++, referring to the symbols defined in the C objects via the extern keyword, but you can't do the opposite