how to compile code using external dll library - c++

I want to compile program with one file test.cpp, which use external library (i have dll and lib). How to do that?
My test.cpp
#include <ExternalLib.h>
#include <iostream>
#include <stdio.h>
#define USE_USB_AUTO_CONNECT 1
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
return 0;
}
ExternalLib is provided by third part company with files: ExternalLib.lib and ExternalLib.dll. There are also files with extension so and a.
When I compile with command:
g++ test.cpp
I get error:
test.cpp:1:20: fatal error: ExternalLib.h: No such file or directory
#include <ExternalLib.h>
Thanks for your help.

I think you are trying to use dll files (compiled in Windows) under Linux, that will not work.
Anyway your error is that the compiler isn't able to locate your header file, to solve this you have several options:
1) Provide the include path when compiling using -I switch
i.e.: g++ test.cpp -I<path to your include directory>
2) Copy the ExternalLib.h to the same directory in which test.cpp lies and use #include "ExternalLib.h" instead of #include <ExternalLib.h>
3) Provide relative path to ExternalLib.h starting from the directory in which test.cpp lies. That is, replace #include <ExternalLib.h> for #include "../../external/ExternalLib.h"
This will solve the No such file or directory problem. Then you will also have to provide the binary/library files using -L (for library path) and -l (for library file) switches.

Related

Error compiling C++ source utilizing the Boost.Math library

I'm trying to use a couple of functions from the Boost Math library in some C++ code using the G++ compiler but I've been unsuccessful. This is on macOS.
I downloaded and extracted the Boost tar.gz from here and placed it into my source folder.
Within my C++ I've tried
#include "boost_1_63_0/boost/math/distributions/chi_squared.hpp" and
#include <boost_1_63_0/boost/math/distributions/chi_squared.hpp>.
The quotation version partially works but the chi_squared.hpp file includes fwd.hpp using the bracket (#include <...>) notation and that breaks my compilation with error In file included from main.cpp:9: ./boost_1_63_0/boost/math/distributions/chi_squared.hpp:12:10: fatal error: 'boost/math/distributions/fwd.hpp' file not found #include <boost/math/distributions/fwd.hpp>.
To compile I've used an assortment of commands, all unsuccessfully:
g++ -L /boost_1_63_0/boost/math/distributions main.cpp
g++ -I"/boost_1_63_0/boost/math/" main.cpp
g++ -I "/boost_1_63_0/boost/math/" main.cpp
g++ main.cpp -lboost_math
What is the correct include statement and G++ command that I need to use?
Resolved using
#include "/Users/[me]/[project_dir]/boost_1_63_0/boost/math/distributions/chi_squared.hpp"
and
g++ -I/Users/[me]/[project_dir]/boost_1_63_0/ main.cpp

Calling a method in a static library

I'm trying to write a C standard library from scratch on OSX with gcc. When I try to include a header file from my library in my test program, I get the error that it isn't defined. I try to use the -nostdlib flag but I still can't include my file.
My test program:
#include <math.h>
#include <bool.h>
#include <ctype.h>
#include <string.h>
#include <io.h>
int main(){
int x = sin(0.5);
int y = pow(2,3);
int z = abs(12);
myiofunction(7);
exit(0);
}
math.h,bool.h,ctype.h,string.h, and io.h are defined in my library. What am I doing incorrectly?
EDIT:
The error message that I am getting is:
helloTest.c:10:10: fatal error: 'bool.h' file not found
Header files aren't compiled into a static library. They have to be available to both library and program.
Therefore, when compiling your program, make sure to specify the -I options to let the compiler find your library's header files.
In order for it to use your own standard library as well, you have to use the -I option to include your library:
gcc -nostdlib -I/path/to/my/headers/ ...
So if the headers for those files were located in ./include, you'd compile with:
gcc -nostdlib -I./include/ ....
Of course you need to provide the object code for these functions at some point. Then you can link them all together using ld with -lgcc to resolve any internal GCC subroutines.
GCC Linking Options: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
You need to write :
#include "bool.h"
Check that out:
Include Syntax
If you use it won't work anyway. You have to typed as "bool.h"
and the other thing is -l option.
Check it out : https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
If you specifiy the default location : -R [PATH]
The library to include is
#include <stdbool.h>

Confusion between including header files and source files in main program

I have heard that we should write the declarations in a header file and the definition in a source file, with both the source and the header having the same name. And then just include the header in the source.
Here is an example myFile.h:
void printer (void);
Here is the implementation of printer in myFile.cpp:
#include "myFile.h"
#include <iostream>
using namespace std;
void printer ()
{
cout<< "I am a printer";
}
Here is my main.cpp:
#include "myFile.h"
int main ()
{
printer();
return 0;
}
Now when I run the program, I get the expected error: undefined reference to printer. But when I see code on github or other projects I see that usually they have included the header file and not the source file. I also tried using the header guards ifndef but still the same error came.
The main program is successfully compiled if:
If i include myFIle.cpp in myFile.h
If i include just myFile.cpp in main
What I the general practice while doing the same?
You should include your myFile.cpp in the linking process:
g++ myFile.cpp main.cpp
The error message undefined reference to printer is actual a linker error, not a compiler error.
Explanation
If you use only g++ main.cpp compiler won't create code from myFile.cpp. He knows that there should be a function with the signature void printer(void), but he doesn't now yet where this function is. He completely ignores this fact, since you can provide pre-compiled object files ("myFile.o") and link those later:
g++ myFile.cpp -c # compile myFile.cpp
g++ main.cpp -c # compile myFile.cpp
g++ myFile.o main.o # link both files together.
-c will tell g++ only to compile the files, but not link them together to an executable. This is done by a linker (g++ will probably call ld in your configuration). The linker will create an executable which contains all needed libraries like libc++ and actual code.
IDE remarks
If you use an IDE make sure that all needed files are included in the project. This includes all header and source files and linkage options for additional libraries.
When yourself define a header file and want to include it, you should enclose it "", such as :
#include "myFile.h"
#include "myFile.h" // would be better.
It seems you forgot the " surrounding the include.
You should use
#include "myFile.h"
or
#include <myFile.h>
the later is rather for system libraries. Both forms differ in the way the search the file.
You find more details on
http://msdn.microsoft.com/en-us/library/36k2cdd4%28v=vs.71%29.aspx

hello world python extension in c++ using boost?

Here's my simple first attempt at a python extension using boost. Can someone help me to understand what's causing the compilation error?
#include <iostream>
using namespace std;
void say_hello(const char* name) {
cout << "Hello " << name << "!\n";
}
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("say_hello", say_hello);
}
user#host:~$g++ main.cpp -o test.so
In file included from /usr/include/boost/python/detail/prefix.hpp:13:0, from /usr/include/boost/python/module.hpp:8, from main.cpp:8:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory compilation terminated.
/usr/include/boost/python/detail/wrap_python.hpp:50:23:
fatal error: pyconfig.h: No such file
or directory compilation terminated.
This line tells exactly why it doesn't work. Your compiler doesn't know where is the pyconfig.h file. You have two options here:
place pyconfig.h in a location that
g++ knows about (i.e. your
project's directory)
add -I DIRECTORY (this is capital i,
not lowercase L) flag to g++ that
will make g++ search DIRECTORY for header files
g++ -I /path/to/my/include/files main.cpp
If you face this problem in your NetBeans then just add "/usr/include/python 2.7/" folder in your NetBeans additional include option. You will get this additional include option in properties.
You need to place pyconfig.h in same directory

hypertable library linking problem

i have installed hypertable in /opt/hypertable/current/ and i run an example program from hypertable...
#include <Common/Compat.h>
#include <iostream>
#include <fstream>
#include <string>
#include <Common/System.h>
#include <Common/Error.h>
#include <Hypertable/Lib/Client.h>
#include <Hypertable/Lib/KeySpec.h>
using namespace Hypertable;
int main(int argc, char* argv[]) {
ClientPtr client_ptr;
TablePtr table_ptr;
TableMutatorPtr mutator_ptr;
KeySpec key;
const char* install_dir = "/opt/hypertable/current/";
client_ptr = new Client( System::locate_install_dir(install_dir) );
}
i got this error
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testes.d" -MT"src/testes.d" -o"src/testes.o" "../src/testes.cpp"
../src/testes.cpp:1: fatal error: Common/Compat.h: No such file or directory
i used eclipse CDT for my development and i linked using project Properties->c/c++build->setting->Libraries->LibrarySetPath(-L) and i have inked the HyperCommon also in -l this i set it as /opt/hypertable/current/include/ can any one tell me y i am getting this error...
There are two different paths you need to set when building software: the include path and the library path. You seem to be confusing them.
The include path is the path to find all the .h files. If you have an include path problem, it will manifest at compile time (when building each individual .o file), which is what you are seeing. "Common/Compat.h: No such file or directory" means you are likely missing an include path.
The library path is the path to find the DLL/shared object files at link time. If you have a library path problem, it will manifest at link time (when creating the final executable from the .o files). You are not up to that stage of compilation.
So doing LibrarySetPath and setting -l or -L is a linker/library thing; you want to fix the include path.
Most likely, you want to add /opt/hypertable/current/include/ to the include path (in Eclipse). On the GCC command line, this will be done with -I /opt/hypertable/current/include/, NOT with -L.
you want to add /opt/hypertable/current/include/ThriftBroker/gen-cpp to the include path
你还得一起编译/opt/hypertable/current/include/ThriftBroker/gen-cpp下的cpp文件