How to compile curlpp on ubuntu? - c++

Below is a simple test.c code using curl:
#include <stdio.h>
#include <curl/curl.h>
int main(){
return 0;
}
To compile this code I use:
gcc test1.c -lcurl -o test1
For test1.c above compilation is correct. Now I would like to write some code using C++ libs (curlpp) and after that compile it.
#include <iostream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
int main(){
return 0;
}
To compile this code I tried:
g++ test2.cpp -lcurl -o test2
But I get this error:
fatal error: curlpp/cURLpp.hpp no such file or directory
compilation terminated.
This formula is not correct. The question is how to compile second code - test2.cpp?

You most likely forgot to install libcurlpp-dev.
You can find out where the required header files are located by running:
$ dpkg -S cURLpp.hpp
libcurlpp-dev:amd64: /usr/include/curlpp/cURLpp.hpp

Related

"./main: not found" (C++, RP 3/B, Geany IDE)

I'm attempting to write a simple program that calls a function written in a pair of Header and CPP files.
I'm doing this on a Raspberry Pi 3 Model B, and the Geany IDE v1.37.1.
Compile Command:
g++ -Wall -c "%f" -c test.cpp
Build Command:
g++ -Wall -o "%e" "%f" -o test test.cpp
main.cpp:
#include "test.h"
int main()
{
test_function();
return 0;
}
test.h:
#ifndef _test_h_
#define _test_h_
#include <iostream>
void test_function();
#endif
test.cpp:
#include "test.h"
void test_function()
{
std::cout << "hello world";
}
The code above compiles & builds fine, however attempting to run it yields the following error:
./main: not found
(program exited with code: 127)
Perhaps I am messing something up with the Compile & Build Commands?
Thank you for reading my post, any guidance is apprecaited!
Notice the compile command:
-o test
This means that the output binary will be test, so you can execute the application in your terminal or shell via ./test.

Undefined symbols for architecture x86_64: linker error

I'm trying to do a test of basic linking for cpp files, I've been searching all over and am having a lot of trouble trying to find a solution. I understand that I have to include the header in both cpp's, but I'm having trouble trying to run these two together.
//testMain.cpp
#include <iostream>
#include <stdio.h>
#include "func.h"
using namespace Temp;
int main()
{
getInfo();
return 0;
}
//func.h
#ifndef FUNC_H
#define FUNC_H
#include <iostream>
#include <stdio.h>
namespace Temp{
int getInfo();
}
#endif
//functions.cpp
#include "func.h"
using namespace std;
int Temp::getInfo()
{
return 5 + 6;
}
//error that I'm getting using VS Code
cd "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/" && g++ testMain.cpp -o testMain && "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/"testMain
Undefined symbols for architecture x86_64:
"Temp::getInfo()", referenced from:
_main in testMain-1f71a1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You're supposed to specify all translation unit files when linking a C++ program.
Your program consists of two source files, testMain.cpp and functions.cpp.
Hence the compile-and-link command should be something like:
g++ testMain.cpp functions.cpp -o testMain
Alternatively you can compile each source into separately and then link them into an executable:
g++ -c testMain.cpp -o testMain.o
g++ -c functions.cpp -o functions.o
g++ testMain.o functions.o -o testMain
Having some kind of a Makefile helps to automate this.

C++ gcc Namespace not found

I know there are many similar topics but there are equally many unique mistakes that may lead to this problem (so I think). Therefore I ask, after some research.
My problem is that the compiler, GNU GCC, when compiling one file does not see my namespace declared in another file. The IDE (CodeBlocks) evidently does see it as it auto-completes the name of the namespace. I tried to isolate the problem and came up with this:
File main.cpp:
namespace MyName
{
int MyVar;
}
#include "T1.cpp"
int main()
{
return 0;
}
File T1.cpp:
using namespace MyName;
error: 'MyName' is not a name-space name.
In my project I have a header file, say T1.h, and an implementation file T1.cpp — and MyName isn't accessible in either of them.
Any help or guidance would be appreciated.
What's happening is that CodeBlocks is compiling both main.cpp and T1.cpp. Here is what happens when you try to compile each one:
main.cpp:
$ g++ main.cpp
$
T1.cpp
$ g++ T1.cpp
T1.cpp:1:17: error: ‘MyName’ is not a namespace-name
using namespace MyName;
^
T1.cpp:1:23: error: expected namespace-name before ‘;’ token
using namespace MyName;
^
$
T1.cpp, when compiled on it's own, has no knowledge of MyName. To fix this, don't include .cpp files, and put your declarations in header files.
Edit: From what I gather, this may be a better way to organize your example:
T1.h:
namespace MyName {
extern int MyVar;
}
T1.cpp
#include "T1.h"
int MyName::MyVar = 5;
main.cpp
#include "T1.h"
#include <iostream>
using namespace MyName;
int main()
{
std::cout << MyVar << std::endl;
return 0;
}
Now it will compile correctly:
$ g++ -c T1.cpp -o T1.o
$ g++ -c main.cpp -o main.o
$ g++ T1.o main.o
$ ./a.out
5

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.

LLVM JIT-compiled program cannot find external functions

My program which JIT compiles a LLVM IR module and calls a function foo defined therein fails at runtime if foo uses an externally-defined function:
LLVM ERROR: Program used external function 'glutInit' which could not be resolved!
My program:
// foo1.cpp
#include <GL/glut.h>
extern "C" void foo()
{
glutInit(0,0);
}
// foo2.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <llvm/Support/raw_ostream.h>
#include <llvm/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/IRReader.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/ExecutionEngine/RuntimeDyld.h>
int main(int argc, char **argv)
{
using namespace llvm;
InitializeNativeTarget();
LLVMContext context;
SMDiagnostic error;
std::ifstream ir_file("foo1.s");
std::string ir((std::istreambuf_iterator<char>(ir_file)),
(std::istreambuf_iterator<char>()));
Module *m = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir)), error, context);
if(!m)
{
error.print(argv[0], errs());
}
ExecutionEngine *ee = ExecutionEngine::create(m);
Function *func = ee->FindFunctionNamed("foo");
if(func == 0)
{
std::cerr << "Couldn't find Function foo" << std::endl;
std::exit(-1);
}
typedef void (*fcn_ptr)();
fcn_ptr foo = reinterpret_cast<fcn_ptr>(ee->getPointerToFunction(func));
foo();
delete ee;
return 0;
}
Here's how I build my program:
$ clang -S -emit-llvm foo1.cpp
$ g++ -rdynamic foo2.cpp `llvm-config --cxxflags` `llvm-config --libs` `llvm-config --ldflags` -lglut
The output:
$ ./a.out
LLVM ERROR: Program used external function 'glutInit' which could not be resolved!
It fails with a similar error any time I try to use an externally-defined function which is not in the C++ standard library (e.g., printf, malloc, & free are no problem). What am I doing wrong?
Make sure that glutInit was linked into a.out. If your host code (the thing executing the JIT) didn't call it, it could have been nixed by the linker. If that's the case, you have to add a dummy reference to it or use linker scripts / flags.
Adding the command line option -Wl,-no-as-needed immediately before -lglut will prevent the linker from dropping the glut library, which it otherwise thinks is not needed:
$ g++ -rdynamic foo2.cpp `llvm-config --cxxflags` `llvm-config --libs` `llvm-config --ldflags` -Wl,-no-as-needed -lglut