How to compile your own C-code as part of ModemManager - c++

I want to write simple C codes to use the functions in ModemManager 1.4.12 to use some of the functions provided in ModemManager to do modem related functions.
I have added the headers I need:
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <glib.h>
#include <gio/gio.h>
#include <libmm-glib.h>
#include "mmcli.h"
#include "mmcli-common.h"
Compile with gcc -o test test.h
but it complains that glib.h is not found.
When I compile with:
gcc -Wall pkg-config --cflags libnm pkg-config --cflags --libs gio-2.0
it complains that fatal error: libmm-glib.h: No such file or directory
When I use -I to include libmm-glib.h, it complains that ModemManager.h is not found.
Should I keep Adding directories with -I or is there a more proper way of doing it?
Thanks

It is not clear what you want to accomplish...
Do you want to build a separate program that uses libmm-glib to talk to ModemManager via DBus? If so:
/* save as test.c and compile with:
* $ gcc -o test `pkg-config --cflags --libs mm-glib` test.c
*/
#include <libmm-glib.h>
int main (int argc, const char **argv)
{
...
}
Note that pkg-config requesting mm-glib cflags/libs should be enough, as that will pull any additional dependency cflags/libs, like glib/gobject/gio.
But from the looks of your code sample, you're also adding mmcli specific headers... so do you want to extend mmcli with new capabilities? If so, instead of giving custom gcc commands, you should extend the mmcli sources and if you need to add new files in the compilation of mmcli, just modify the Makefile.am under cli/.

Related

is there any way that I can add my own custom c/c++ header file to where the standard header functions come from so that I can use them at any time [duplicate]

I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c

include botan 2 in compilation

i try include some botan header in compilation process
#include <botan/rng.h>
#include <botan/auto_rng.h>
#include <botan/cipher_mode.h>
#include <botan/hex.h>
#include <iostream>
int main(int argc, char** argv)
{
return 0;
}
I found that i need to compile with following command in order to build successfully
g++ app.cpp -I/usr/local/include/botan-2
i saw some folks execute
g++ app.cpp -lbotan-2
i tried it out but i get a error
'app.cpp:1:10: fatal error: botan/rng.h: No such file or directory
#include <botan/rng.h>
Am i missing anything ?
The following command:
g++ app.cpp -lbotan-2
links botan-2 with app.cpp but you still need to specify where to find the headers:
g++ app.cpp -I/usr/local/include/botan-2 -lbotan-2
Under my system, the headers for botan-2 are in /usr/include/botan-2. So, make sure you are giving the right path.

How to use png++ (C++ library) on Windows?

I am trying to use this C++ library (png++) on Windows, but I unable to compile any program when I use it. Example of Code I am using to test:
#include <png++/png.hpp>
#include <png.h>
int main(){
//anything
}
When I try to compile using g++ -I path/png++ main.cpp -o main, I get
fatal error: png++/png.hpp: No such file or directory
#include <png++/png.hpp>
I understand png++ depends on libpng, I tried adding it as an I- flag, i.e. compile using
g++ -I path/png++ -I path/libpng main.cpp -o main, but it doesn't resolve the issue, png.h is found by the compiler but not png++/png.hpp.
I hope someone will be able to help.
Thanks!

Using Lua with C++

I'm trying to embed lua in a c program, but I'm having problems to compile the code. I have installed everything lua 5.2 related in synaptic and when tried to compile this:
extern "C"{
#include <stdio.h>
#include <string.h>
#include "lua5.2/lua.h"
#include "lua5.2/lauxlib.h"
#include "lua5.2/lualib.h"
}
int main(int argc, char* argv[])
{
lua_State *lua_state;
lua_state = luaL_newstate();
lua_close(lua_state);
}
and compile using
g++ main.cpp -llua
show the folowing errors
Could not find -llua
what do?
There are tools you can use to find the proper compiler / linker switches for a library.
In particular, with a proper installation of the lua5.2 libraries you can use
pkg-config -libs lua5.2
On my system it outputs
-llua5.2
Use this, or the output of pkg-config (backticked) as your linkers argument.
Of course, pkg-config can also tell you the -CFLAGS for the package with
pkg-config --cflags lua5.2
The man page is quite readable.

Undefined Reference Compiler Error

I think I'm getting close, but I'm having this error I've been banging my head against the wall on for hours. I'm missing something stupid, and I've gone character by character but I can't find it.
The compiler is giving me
main.cpp:16: undefined reference to `translator::translator(std::istream&)'
collect2: error: ld returned 1 exit status
when I try to compile my program. The command I'm using to compile is:
clear && g++ -g -Wall main.cpp -o Pear
The three sections of use are as follows:
main.cpp
int main(int argc, char* argv[])
{
std::ifstream myFile;
myFile.open(argv[1]);
translator example(myFile);
myFile.close();
return 0;
}
translator.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "translator.h"
translator::translator(std::istream& in)
{
table1(in);
table2(in);
}
translator.h
#ifndef TRANSLATOR
#define TRANSLATOR
#include <fstream>
#include <iostream>
#include <string>
#include "translationTable.h"
class translator
{
private:
translationTable<std::string, int> table1;
translationTable<int, std::string> table2;
translator();
public:
translator(std::istream& in);
};
#endif
Any ideas? I've tried so much, and I've looked up similar problems, but they all have different sources. Thanks in advance!
The command line for g++ needs to include both source files, like this:
g++ -g -Wall main.cpp translator.cpp -o Pear
Otherwise, the compiler has no idea from where to get the implementation of the translator::translator(std::istream&) member function.
*EDIT: * (from the comment)
I thought that basically the use of header files was so that it would know where to get each implementation of the file?
This part is grossly oversimplified, but it should help you get the picture. Recall that the process of producing an executable from C++ sources consists of two major steps - compilation and linking. The g++ program performs them both (it can do just one if you specify -c flags, or pass only .o files).
The compiler and the linker stages of g++ do not "talk" to each other directly. The compiler produces the inputs for the linker, and that's where the communication ends.
Header files are for the compiler. Specifically, they are for the first stage of compilation - the preprocessing. Once the preprocessor has finished, there is no knowledge of where the definitions came from. Even the compiler does not know it, let alone the linker. That is why you need to "help" the linker by supplying all the relevant sources to g++.
You aren't linking translator.o with your application.
g++ -g -c translator.cpp
followed by
g++ -g -Wall main.cpp translator.o -o Pear