title: How to access a shared library file with mkmf in Ruby?
body: <p>I am trying to work with <code>libcsv</code> here. The header file <code>csv.h</code> is in <code>/usr/include</code> directory, and the shared library file <code>libcsv.so</code> in <code>/usr/lib64</code> (Fedora 35).</p>
  <p>For now, I was able to make the gem file work using the shell command:</p>
  <pre class="lang-sh prettyprint-override"><code>gcc -c -fPIC rcsv.c -o rcsv.o
  gcc -shared -lcsv rcsv.o -o rcsv.so
  </code></pre>
  <p>I have to manually move the .so file to <code>lib/rcsv/</code>, which is quite inconvenient, and is not favourable for installing gems.</p>
  <p>I would like to write the same in code in Ruby format inside <code>extconf.rb</code>. How should I go about this?</p>
  <p><strong>extconf.rb</strong></p>
  <pre class="lang-rb prettyprint-override"><code>require 'mkmf'
  
  have_header(&quot;csv.h&quot;)
  have_library('libcsv.so')
  
  create_makefile('rcsv/rcsv')
  </code></pre>
  <p><strong>rake log</strong></p>
  <pre class="lang-sh prettyprint-override"><code>(in /home/&lt;username&gt;/Desktop/rcsv)
  mkdir -p tmp/x86_64-linux/rcsv/3.0.3
  cd tmp/x86_64-linux/rcsv/3.0.3
  /home/&lt;username&gt;/.asdf/installs/ruby/3.0.3/bin/ruby -I. -r.rake-compiler-siteconf.rb ../../../../ext/rcsv/extconf.rb
  checking for csv.h... yes
  checking for -llibcsv.so... no
  creating Makefile
  cd -
  cd tmp/x86_64-linux/rcsv/3.0.3
  /usr/bin/gmake
  compiling ../../../../ext/rcsv/rcsv.c
  linking shared-object rcsv/rcsv.so
  cd -
  mkdir -p tmp/x86_64-linux/stage/lib/rcsv
  /usr/bin/gmake install target_prefix=
  /usr/bin/install -c -m 0755 rcsv.so /home/&lt;username&gt;/Desktop/rcsv/lib/rcsv
  cp tmp/x86_64-linux/rcsv/3.0.3/rcsv.so tmp/x86_64-linux/stage/lib/rcsv/rcsv.so
  Loaded suite /home/&lt;username&gt;/.asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/rake_test_loader
  Started
  /home/&lt;username&gt;/.asdf/installs/ruby/3.0.3/bin/ruby: symbol lookup error: /home/&lt;username&gt;/Desktop/rcsv/lib/rcsv/rcsv.so: undefined symbol: csv_init
  rake aborted!
  Command failed with status (127)
  
  Tasks: TOP =&gt; default =&gt; test
  (See full trace by running task with --trace)
  </code></pre>