I’m trying to run a block of Ruby code inside a C program.
I have the following code:
#include <ruby.h>
int main(int argc, char* argv[])
{
/* Construct the VM */
ruby_init();
/* Ruby goes here */
/* Destruct the VM */
return ruby_cleanup(0);
}
But when I try to run the program, I get the following error:
fatal error: ruby.h: No such file or directory
#include <ruby.h>
I read that it is needed to tell the compiler about the include paths for the required headers with the following code in Ubuntu:
pkg-config --cflags --libs ruby-2.5
gcc -I/usr/include/ruby-2.5.0 -I/usr/include/ruby-2.5.0/x86_64-linux -lruby
I have already done that, but the problem isn’t solved.
Here is the link: https://silverhammermba.github.io/emberb/embed/
Follow the steps in How can I include a needed C library using GCC?.
I don't understand the difference between the two -l, but try to follow the following structure. If I'm right, your command will be like:
gcc -I/usr/include/ruby-2.5.0 -L/usr/include/ruby-2.5.0/x86_64-linux -lruby
Where:
-I <searchpath to include files>
-L <searchpath to the lib file>
-l <thelibname>
I don't know if the library is the first or the second parameter, but you can check it.
Related
I cannot make rpath work properly and make my binary to search for the library in the specified folder:
I have 3 very simple files:
main.c
#include <stdio.h>
#include <func.h>
int main() {
testing();
return 1;
}
func.h
void testing();
func.c
#include "func.h"
void testing(){
printf(testing\n");
}
Then I proceed to create a shared library as it follows:
gcc -c -fpic func.c -o ../release/func.o
gcc -shared -o ../release/lib/lib_func.so ../release/func.o
And then compile the program:
gcc main.c ../release/lib/lib_time_mgmt.so -Wl,-rpath=/home/root/ -o ../release/main
I receive the next warning:
main.c:7:2: warning: implicit declaration of function ‘testing’ [-Wimplicit-function-declaration]
testing();
But besides it, the program works fine.
However, my problem is that if now I want to move the library to /home/root (as specified in rpath) it does not work and the library is still searched only in the path specified when I compiled the main.c file which is ../release/lib/lib_time_mgmt.so
What am I doing wrong?
EDIT: After accepting the answer, I leave here the exact line as I used it and made it work for whoever might find it useful:
gcc main.c -L/home/root -Wl,-rpath,'/home/root/' -l:libtime_mgmt -o ${OUT_FILE}
Note: the rpath was used with the path betwen simple '. Not sure if that was the reason why it was not working before, but it worked this way now.
rpath is not used at compile time, but rather at link/runtime... thus you probably need to use both of these:
-L /home/root - to link correctly at build time
-Wl,-rpath=/home/root - to link correctly at run-time
You should use the -l ${lib} flag to link with libraries, don't specify their path as an input.
In addition to this, convention states that the libraries are named libNAME.so - e.g:
-l func will try to link with libfunc.so
-l time_mgmt will try to link with libtime_mgmt.so
Once you've addressed the above points, try the following:
gcc main.c -L/home/root -Wl,-rpath=/home/root -lfunc -ltime_mgmt -o ${OUT_FILE}
As a final point, I'd advise that you try not to use rpath, and instead focus on installing libraries in the correct places.
Unrelated to your question, but worth noting. Your use of #include <...> vs #include "..." is questionable. See: What is the difference between #include <filename> and #include "filename"?
First of all thanks for your attention and help.
I have been trying to ./configure and build a program that use a lot of libraries, and specifically libudns. I have been installing several libraries that were needed or by apt-get or by compile, and all of them works, but libudns (which is freaking me out).
This program make use of a configure script that include the following lines of code that add the flag -ludns to the Makefile:
if [ "x$WITH_UDNS" == "xy" ]; then
mkl_lib_check "udns" HAVE_UDNS fail CC "-ludns" \
"#include <udns.h>
void *f();void *f(){return dns_init;}"
fi
When I type ./configure the script checks if all libraries are presents on the system by pkg-config and by compile, as you can see in the following snap:
As you can see, configure cannot see this library.
What I have done is try to install this package by the two different ways:
By pkg-config: sudo apt-get install libudns-dev
By compile:
git clone https://github.com/shadowsocks/libudns
cd libudns
./autogen.sh
./configure && make
sudo make install
With this two ways, I have both:
/usr/lib/x86_64-linux-gnu/libudns.so
/usr/local/include/udns.h
These paths are the same where others libraries are installed, for example PostgreSQL, which you can that is detected:
/usr/lib/x86_64-linux-gnu/libpq.so
/usr/include/postgresql/libpq-fe.h
But the result is always the same:
Does anybody knows how to link this library?
Also, I have try these other things:
Copy udns.h to /usr/include: sudo cp /usr/local/include/udns.h /usr/include/udns.h
Make a sample program only to emulate this check:
#include <stdio.h>
#include <udns.h>
struct dns_ctx* ctx;
void *f();
void * f(){return dns_init;}
int main(int argc, char** argv){
int do_open = 0;
printf("Hola, mundo\n");
f(ctx, do_open);
}
And when I try to build this program with:
gcc main.c -o hello_world -ludns
it WORKS!?!
I have also try to build this program without the -ludns flag, and it gives me the same error as before:
So, I do not understand where is the fail, considering that as you can see in the second image, the -ludns flag is present.
Thanks a lot for your time. Any advise will be welcomed.
I'm trying to compile a program with gcc, but I keep getting an error that it can't find the files I've specified to include.
My code (a simplified version of what I'm trying to do which highlights the issue):
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
}
I'm compiling with gcc, using:
gcc test.c -I~/gtk/inst/include/gtk-2.0
which gives me
test.c:1:21: error: gtk/gtk.h: No such file or directory
However,
ls ~/gtk/inst/include/gtk-2.0/gtk | grep gtk.h
returns
gtk.h
gtkcheckbutton.h
gtkcheckmenuitem.h
gtkshow.h
so I'm not sure where I'm messing up.
My OS is Mac OSX 10.8.5, and GCC is version 4.2.1.
You should either:
Put a space in front of the tilde:
-I ~/gtk/inst/include/gtk-2.0
Use the absolute path:
-I /users/username/gtk/inst/include/gtk-2.0
or use $HOME:
-I $HOME/gtk/inst/include/gtk-2.0
Shell only does tilde expansion if the ~ is the first nonquoted character in a word. The way you've got it now, shell has no clue that this is trying to point to the users home directory.
I have a sample C project that use GLib Library. In that source code, it use :
#include <glib.h>
When I compile, I found this error : "Glib.h : no such file or folder". I have google and find out that I should install this lib. So I use those command:
apt-get install libgtk2.0-dev
apt-get install glade
After that, I have checked and see already exist this header file in my system: usr/include/glib-2.0/glib.h But when I compile, I still meet problem above.
So I have change include line to :
#include <glib-2.0/glib.h>
So, after that, when I compile, I meet error inside glib.h header :
#ifndef __G_LIB_H__
#define __G_LIB_H__
#define __GLIB_H_INSIDE__
#include <glib/galloca.h>
#include <glib/garray.h>
// more code here
glib/galloca.h : no such file or directory. Because this error is inside system header file, I cannot modify anymore and still cannot compile.
I don't know how to fix this. I have read some post, that they change makefile. But, because my project is compiled automatically by IDE (CodeBlock) and I cannot really write a makefile, so that method doesn't suitable for me.
Please tell me a way to fix this.
Thanks :)
There must be some problem with how you build. To compile C programs that use GLib, you need package libglib2.0-dev. You can either install it directly, or install libgtk2.0-dev, which pulls it in as a dependency. So you have the packages you need.
The correct way to compile a GLib program is to use -I with the path to the GLib include files.
An example (from How to compile a helloworld GLib program? on askubuntu):
gcc $(pkg-config --cflags --libs glib-2.0) hello_glib.c
This should let you compile this program:
#include <stdio.h>
#include <glib.h>
int main(int argc, char** argv) {
GList* list = NULL;
list = g_list_append(list, "Hello world!");
printf("The first item is '%s'\n", g_list_first(list)->data);
return 0;
}
The errors you are getting indicate that you are not setting the include path (-I) correctly. How to do this depends on your build system/IDE.
In Code::Blocks, you must set the include path and the linker options in the appropriate configuration dialog. Run pkg-config --cflags --libs glib-2.0, which will output something like
-I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -lglib-2.0
The directories after -I must be set in the compiler options of your project (should be under Project -> Build Options -> Search Directories), and the names after -l must be set in the linker settings. Another option is to create a Makefile, and let Code::Blocks use that.
See e.g. Q: What do I need to know when using 3rd party libs? in the Code::Blocks Wiki.
You should not alter your source code (e.g. the #include directives).
You just need to use pkg-config (both for compiling, with --cflags, and for linking, with --libs), preferably with a builder program like make.
This is an example for exactly your situation: a Makefile using pkg-config to compile some source program using glib
I'm using emacs with flycheck to check C source code syntax and trying to get it working with glib. My code compiles and runs correctly, however flycheck reports a file not found error in the #include <glib.h> line and stops reporting further errors, defeating its purpose.
Here's my sample source file:
#include <stdio.h>
#include <glib.h>
GList* list = NULL;
int main() {
list = g_list_append(list, "a");
list = g_list_append(list, "b");
list = g_list_append(list, "c");
for ( ; list!=NULL; list=list->next) {
printf("%s\n", (char*)list->data);
}
return 0;
}
And the makefile
P=glist
OBJECTS=
CFLAGS=-g -Wall -O3 `pkg-config --cflags glib-2.0`
LDLIBS=`pkg-config --libs glib-2.0`
CC=gcc-4.9
$(P): $(OBJECTS)
If I change the include line to read #include <glib-2.0/glib.h> I get the following error reported in the minibuffer:
Checker c/c++-clang returned non-zero exit code 1, but no errors from
output: In file included from
/var/folders/f/ts3zs3cjbq1fqfhdfrl1w0000gn/T/flycheck87881gVK/glist.c:2:
/usr/local/include/glib-2.0/glib.h:32:10: error: 'glib/galloca.h' file
not found with include; use "quotes" instead In file included
from
/var/folders/_f/ts3_zs3cjbq1fqfhdfrl1w0000gn/T/flycheck87881gVK/glist.c:2:
In file included from /usr/local/include/glib-2.0/glib.h:32:
/usr/local/include/glib-2.0/glib/galloca.h:34:10: fatal error:
'glib/gtypes.h' file not found
Checker definition probably flawed.
Still the code compiles and runs correctly. I'm not sure why it can't find glib/gtypes.h as it exists under one of the included directories. The output from pkg-config --cflags glib-2.0 is:
-I/usr/local/Cellar/glib/2.36.4/include/glib-2.0 -I/usr/local/Cellar/glib/2.36.4/lib/glib-2.0/include -I/usr/local/opt/gettext/include
Listing ls /usr/local/Cellar/glib/2.36.4/include/glib-2.0/glib/gtypes.h
/usr/local/Cellar/glib/2.36.4/include/glib-2.0/glib/gtypes.h
And listing ls /usr/local/include/glib-2.0/glib/gtypes.h
/usr/local/include/glib-2.0/glib/gtypes.h
So the file is there. I wouldn't mind switching to flymake if flycheck is to blame, but I'm not sure if it's a problem with my setup or flycheck itself. Plus flycheck configuration is dead simple and otherwise works very well. I'm using version 20130904.2245 installed from elpa.
Flycheck does not use Makefiles, nor does it attempt to parse them. I cannot help but wonder how you even got this idea, given that no such behaviour is documented in the manual, and no syntax checker for make files even exists.
Flycheck runs Clang directly. You need to explicitly configure the include path for syntax checking by setting flycheck-clang-include-path accordingly. You must do so yourself, this is not done automatically based on your Makefile.
You can set the path via file/dir local variables, or write some custom Emacs Lisp code to parse your Makefile.