简体   繁体   中英

Running Ruby in C compile and link issues

I've been trying all day to build and run a simple Ruby inside of C program.

This is a recurring topic here, and none of them are identical to my issue nor have any of the solutions helped me. I have the ruby-dev installed.

The pkg-config command gives this:

  $ pkg-config --cflags --libs ruby-2.7
  -I/usr/include/x86_64-linux-gnu/ruby-2.7.0 -I/usr/include/ruby-2.7.0 -lruby-2.7 -lm

The compile command gives this:

   $ gcc -I/usr/include/x86_64-linux-gnu/ruby-2.7.0 -I/usr/include/ruby-2.7.0 -I. -lruby-2.7 -o hello *.c

   /usr/bin/ld: /tmp/ccdKXtnU.o: in function 'main': 
   hello.c:(.text+0x9): undefined reference to 'ruby_setup' 
   collect2: error: ld returned 1 exit status

I have tried switching up the order of the includes. I have tried removing one then the other include. I have tried using a Makefile and running it thru make. I have tried breaking the program up into multiple files. I have tried symbolically linking the architecture relative config.h file into the main header file directory.

The only thing I can think of that I haven't tried is putting the name of the ruby object library that needs to be linked in on the command line, but I don't know the name, or location, of that file.

Here's the latest rendition of the program:

  #include <stdio.h>
  #include <ruby.h>

  int main(void)
  {
    if (ruby_setup()){
            puts("Hola!");
    }
    else{
            printf("Hello World\n");
    }
    return(0);
  }

One of the reasons that pkg-config separates cflags and libs is that they go in different places in the command-line (and sometimes different commands).

If you're going to compile and link in one command, it goes like this:

c99 -o hello $(pkg-config --cflags ruby-2.7) *.c $(pkg-config --libs ruby-2.7)

There's a certain logic to this arrangement. First, we tell the compiler where to look for header files (which it must see before it compiles your program), then where to find the program to compile, and finally where to find the libraries which are referred to by the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM