Compile C++ code in Ubuntu with gcc linking a library - c++

I'm stuck in a very simple problem: I cannot manage to make work my simple code example in C++.
I want to include the "curl" library but when I compile with the command:
g++ -o myprog.out myprog.cpp -L/curl/include/ -lcurl
I get the following error message:
myprog.cpp:3:71: fatal error: /curl/include/curl/curl.h: No such file
or directory
My folder contains:
myprog.cpp (the file I want to compile)
curl -> include -> curl -> curl.h (path in which the curl.h file is located).
My headers file are configured in this way:
include<iostream>
include<string>
include<curl.h>
What I'm doing wrong? It's probably a very simple problem but it's driving me crazy :-/

Change #include <curl.h> to #include <curl/curl.h>.
Change -L/curl/include/ to -I/curl/include.
Add -L/curl/lib -Wl,-rpath=/curl/lib (or whatever the path to curl built libraries).

Related

gcc can't find libcurl header file in the directory it is located

I am trying to compile a cpp using with the following command:
g++ -IC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include\curl -LC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\lib program.cpp
this program has a header file which uses libcurl. the curl library is at -
C:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include\curl\curl.h
gcc gives the following error even though curl.h is in the path -I
mylibrary.h:26:10: fatal error: curl/curl.h: No such file or directory
#include <curl/curl.h>
what am I doing wrong?
The error message means the file curl/curl.h could not be found in the search path specified by -I. You updated the question with the path to the file so the command should be:
g++ -IC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include -LC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\lib program.cpp
Also, can you please try using \ instead of / as path separator in your mylibrary.h file:
#include <curl\curl.h>

How to run a .cpp file with Gecode Framework on Linux?

I use Ubuntu 18.04 and try to compile the example 'money.cpp' file that Gecode brings. I downloaded (gecode-release-5.1.0.tar.gz) and extract it. Then to install Gecode I do the following steps:
(1) ./configure
(2) make
I get satisfactory installation.
Then I try to compile money.cpp, like this:
$g++ money.cpp
and I get the following error:
fatal error: gecode / driver.hh: No such file or directory
  #include
           ^ ~~~~~~~~~~~~~~~~~
I know the problem is that it does not recognize the libraries, but how can I make my .cpp or compile phase recognize them?
regards!
Alberto
You need to call g++ with the -I flag so that g++ knows where to look for the include files:
g++ -I<dir> money.cpp
<dir> is the source code directory for the files (the one with structure as seen on GitHub):
https://github.com/Gecode/gecode
See Compiling with g++:
https://courses.cs.washington.edu/courses/cse373/99au/unix/g++.html

Error message while compiling a program

I’m a newbie to C++ and Linux. There is this code I’m playing with that requires me to install the HElib (Homomorphic encryption library and other libraries - GMP, NTL) which I did. I want to compile the code (main.cpp) that has a header file (FHE.h) in HElib. My problem is how can I link FHE.h (in HElib folder) and main.cpp (in another folder) together so that I can compile them. I have tried some commands
g++ -I/Home/HElib/src/FHE.h main.cpp -o main
Error message
main.cpp:1:17: fatal error: FHE.h: No such file or directory
compilation terminated.
Another command line
g++ -I/Home/HElib/Src/FHE.h -I/Home/SimpleFHESum-master/SimpleFHESum-master/main.cpp -o main]
Error Message
g++: fatal error: no input files
compilation terminated.
What's wrong and how can I fix this?
The -I flag adds the following directory to the include path of the compiler. This enables you to write e.g. #include "FHE.h" even though that file is not located in the same folder as the source file you're trying to compile.
Have you tried just removing the 'FHE.h' part from your -I directive?
g++ -I/Home/HElib/src ...

compiling C++ mac, look for header files not found

Based on this tutorial:
http://syskall.com/how-to-roll-out-your-own-javascript-api-with/index.html/
I am trying to compile a C++ program on a mac, however the includes in my C++ file are not being found. I have the following directory structure:
myProj/
|-- deps/ # third party code
| `-- v8
`-- src/
`-- myProj.cpp
in the myProj.cpp, I have several includes:
#include <include/v8.h>
so when i go to compile, I use the following:
g++ src/jsnotify.cpp -Ideps/v8/include
the deps/v8/include directory clearly has v8.h, but it still shows up as not found. is -I the correct flag for mac? I am also having trouble in linking:
g++ src/jsnotify.cpp -Ideps/v8/ -Ldeps/v8/ -lv8 -lpthread -v
the -lv8 causes:
ld: library not found for -lv8
clang: error: linker command failed with exit code 1
Look at exactly what you're telling the compiler:
#include <include/v8.h>
"open the file "include/v8.h"
g++ src/jsnotify.cpp -Ideps/v8/include
"When trying to find files to include, search in deps/v8/include"
So, the obvious question: does deps/v8/include contain include/v8.h? In other words, do you have the file deps/v8/include/include/v8.h?
As you have it, the pre-processor is trying to resolve #include <include/v8.h> to deps/v8/include/include/v8.h.
Change your include to be:
#include <v8.h>
Or change your compiler command line to:
g++ src/jsnotify.cpp -Ideps/v8
Either option is likely to work - but if v8.h also specifies additional include files specified by prepending the "include" path (e.g. #include <include/foo.h>) then the second option is more likely to work.

Linking with libconfig in c++ on OSX

I have a config file that contains:
#include "libconfig.h++"
I have installed libconfig via homebrew and I am trying to compile my c++ program so that I can use the library but I am having trouble linking to it.
The location of the libconfig .a files is located at /usr/local/Cellar/libconfig/1.4.9/lib/
The documentation says: To link with the library, specify ‘-lconfig++’ as an argument to the linker.
So I have been trying variations on g++ config.cpp -L /usr/local/Cellar/libconfig/1.4.9/lib -lconfig++ -o out.o
But I getting the same error message:
config.cpp:4:10: fatal error: 'libconfig.h++' file not found
#include "libconfig.h++"
Can someone please explain what I am doing wrong?
There is nothing about linker. The compiler says that it can't find the file you include in your cpp. If you have installed libconfig correctly, changing #include "libconfig.h++" to #include <libconfig.h++> will solve the problem. If it does not help, it would mean that there is no "libconfig.h++" in your include path.