I'm working on a Node.js wrapper module for a colleagues C library. The library is created in Shared Object (.so) form for dynamic linking.
My CPP module file begins with
#include "path/to/lib/source/lib.h"
and is built with the following wscript
def set_options(ctx):
ctx.tool_options('compiler_cxx')
def configure(ctx):
ctx.check_tool('compiler_cxx')
ctx.check_tool('node_addon')
ctx.env.append_value('LINKFLAGS', ['-l:lib.so', '-L/path/to/lib.so/'])
def build(ctx):
t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
t.source = ['module.cpp']
t.target = 'module'
When I then proceed to call into my module, which in turns call the library, i get the following error:
node: symbol lookup error: <path/to/module.node>:
undefined symbol: <name of library call>
I tried dumping the dependencies of the module with 'ldd module.node' and I got a little suspicious as it doesn't mention my .so file.
Any help is much appreciated!
Do you know if the dynamic linker can find your library? Try adding the library path to your LD_LIBRARY_PATH. You can run this in the shell before you invoke Node with your test script:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/lib.so/
node test-script.js
(On a Mac, that would be DYLD_LIBRARY_PATH.)
Related
Say, I have a lib package with a C shared library in a sub-directory. This rust library package compiles without errors, by means of tailored build.rs which sub-calls make in the sub-directory and then dynamicly links against the *.so product of make.
The problem arises when I try to link a binary package against this rust lib has the C shared lib within itself.
error: linking with `cc` failed: exit status: 1
= note: /usr/bin/ld: cannot find -lcuda_wrapper
collect2: error: ld returned 1 exit status
The dependency graph can be shown as the following one:
C-shared-lib < rust-lib < rust-bin
And cargo build fails to build rust-bin for not finding that C-shared-lib.
providing LD_LIBRARY_PATH for cargo build neither by command line nor build.rs within rust-bin facilitates the problem and lifts the error.
How to propagate the directory wherein resides libcuda_wrapper.so?
You can link your C library dynamically or statically.
In order to do that you can use add one of the following lines to your build.rs (note that you linkely won't need a lib prefix):
println!("cargo:rustc-link-lib=static=cuda_wrapper");
println!("cargo:rustc-link-lib=dylib=cuda_wrapper");
In order to define a search path, add this line to build.rs
println!("cargo:rustc-link-search=native={}", path);
You would likely want to add it to your rust-lib build.rs. Altenatively, you can create a build.rs for every executable which depends on rust-bin and add the same set of lines here.
It is a good idea to ensure that your C library build output is located under OUT_DIR env variable (you can get it using env::var("OUT_DIR").unwrap() in build.rs). The way cargo clean will work as expected as well.
More details here:
https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib
https://doc.rust-lang.org/rustc/command-line-arguments.html#option-l-link-lib
https://doc.rust-lang.org/cargo/reference/build-script-examples.html#building-a-native-library
https://doc.rust-lang.org/cargo/reference/environment-variables.html
I have read various other posts on SO, but no matter what I do I get the same error. Here is what I have done:
Relevant section of .c file:
__declspec(dllexport) double function(...){...}
I compiled the file with the following commands:
gcc -c mochamath.c
gcc -shared -o mochamath.dll mochamath.o -Wl,--out-implib,libmochamath.dll.a
I have seen some posts saying I need to use the option -mno-cygwin, but when I do that it gives
gcc: error: unrecognized command-line option ‘-mno-cygwin’; did you mean ‘-mno-clwb’?
Compiling without that option, however, seems to work fine.
Here is the python file I am using to test loading the dll:
import os
os.add_dll_directory(os.getcwd())
from ctypes import cdll
mochamath = cdll.LoadLibrary(f"mochamath.dll")
Other posts have also suggested using CDLL instead of cdll.LoadLibrary, but I get the same error either way:
FileNotFoundError: Could not find module 'mochamath.dll' (or one of its dependencies). Try using the full path with constructor syntax.
I have tried various combinations of directory structures, path formatting, etc, but I get the same error no matter what. I must be missing something very basic here, but it is not obvious to me. What am I doing wrong?
I'm trying to make a shared library, in Linux, which is a *.so. My DMD version is 2, latest. I'm just trying yo compile a simple empty shared library, with the code that Mono-D(plugin for MonoDevelop) creates. When I try to compile it, it tells me to check the build log, this is what's in the build log:
Building Solution: QScr (Debug)
Building: QScr (Debug)
Performing main compilation...
Current dictionary: /home/nafees/Desktop/Projects/QScr/QScr
dmd -debug -gc "myclass.d" "dllmain.d" "-I/usr/include/dmd" "-L/IMPLIB:/home/nafees/Desktop/Projects/QScr/QScr/bin/Debug/libQScr.a" "-odobj/Debug" "-of/home/nafees/Desktop/Projects/QScr/QScr/bin/Debug/libQScr.so" -w -vcolumns
/usr/bin/ld: cannot find /IMPLIB:/home/nafees/Desktop/Projects/QScr/QScr/bin/Debug/libQScr.a: No such file or directory
collect2: error: ld returned 1 exit status
--- errorlevel 1
Exit code 1
Build complete -- 1 error, 0 warnings
---------------------- Done ----------------------
Build: 1 error, 0 warnings
This is what dllmain contains:
module dllmain;
And in myclass.d:
module myclass;
class MyClass
{
//TODO: Enter class code here
}
export:
extern(D):
MyClass createMyClass()
{
return new MyClass();
}
I have no idea what is that a file, I'm still fairly new to D, and Linux.
How do I get it to compile? And could someone explain to me what is an .a file?
EDIT: No, it's not a duplicate, I'm trying to compile, while that question is about loading libraries.
EDIT2: I checked the directory, the .a file doesn't exist.
/usr/bin/ld: cannot find /IMPLIB:/home/nafees/Desktop/Projects/QScr/QScr/bin/Debug/libQScr.a: No such file or directory
/IMPLIB is a Windows linker switch. Your IDE is misconfigured (or just buggy).
Try changing the project settings in the IDE or filing a bug against the IDE.
Few things.
extern(D) would not be needed for free functions since it is default
You don't need createMyClass function at all, new will work fine
-shared must be passed to dmd for it to create a shared library
In case you didn't know, you will be passing the files in the shared library as import files when compiling the host binary.
I'm integrating C check framework for my project and I was able to run
autoreconf --install
successfully without any errors.
But when I integrate the C check framework, I'm getting an error and warnings that doesn't make sense.
Here is the error I'm getting
src/Makefile.am:7: warning: variable 'main_LDADD' is defined but no program or
src/Makefile.am:7: library has 'main' as canonical name (possible typo)
tests/Makefile.am:2: error: 'SHELLTESTS_PROGRAMS' is used but 'SHELLTESTSdir' is undefined
tests/Makefile.am:3: warning:variable 'ShellTests_SOURCES' is defined but no program or
tests/Makefile.am:3: library has 'ShellTests' as canonical name (possible typo)
tests/Makefile.am:5: warning: variable ShellTests_LDADD' is defined but no program or
tests/Makefile.am:5:library has 'ShellTests' as canonical name (possible typo)
autoreconf:automake failed with exit status: 1
I've followed the C check example source code, the difference between that and my project is that my class uses other class' methods.
Anyways, this is the Makefile.am under my tests/ that is causing 'havoc' on building the project
TESTS = shelltests
SHELLTESTS_PROGRAMS = shelltests
ShellTests_SOURCES = ShellTests.c $(top_builddir)/src/Shell.h $(top_builddir)/src/Parser.h $(top_builddir)/src/JobControl.h
ShellTests_CFLAGS = #CHECK_CFLAGS#
ShellTests_LDADD = $(top_builddir)/src/libshell.la #CHECK_LIBS#
This is the file structure for my project
src -
Parser.h, Parser.c, Shell.h, Shell.c, Job.h, Job.c, Makefile.am
tests -
ShellTests.c Makefile.am
And this is the code in Makefile.am under src.
lib_LTLIBRARIES = libshell.la
libshell_la_SOURCES = Shell.c Shell.h Parser.h Parser.c JobControl.h JobControl.c
bin_programs = main
main_sources = Main.c
main_LDADD = libshell.la
I followed this user's advice to see if it removes the error: What directory should I use for "error: 'extra_PROGRAMS' is used but 'extradir' is undefined"?
Alas, it does not.
I've tried building the example project - the one that you get when you download under examples, to see if I'm missing anything. But it's not able to build on my machine.
I'm on Mac OS X Mavericks (10.9)
Autoconf 2.69
Automake 1.15
Libtool 2.4.6
Check 0.9.14
I did a couple of things to build this project successfully. I first followed what I outlined here: Check framework example giving me error when running './configure'
Then I followed c check frameworks' naming conventions for the tests source files.
Thanks for the help guys, it gave me some ideas on what the cause of compilation errors.
I am trying to complete the installation for some software. According to which I have to add path. I am not getting it how to do that. Please guide me how to do the following steps.
Add the path to Rpa/Tk headers:
-I/usr/include/rpatk
To link to the Rpa/Tk libraries on Linux add the following link options:
-lrpa -lrvm -lrex -lrlib -lm
RVM library uses some math functions from the system math library, that is why you must include '-lm' to include the math library to your project in addition to the Rpa/Tk built libraries:
librpa librex librvm librlib
http://www.rpasearch.com/rpatk/doc/doxygen/rpadoc/html/rpatk_build.html
this will be within your configure script -- you will need to hack this and add the paths to where gcc/cc gets called
give the link to the actual tar.gz
looking at the example code: http://www.rpasearch.com/rpatk/doc/doxygen/rpadoc/html/js-tokenizer_8c-example.html
gcc -I/usr/include/rpatk -o js-tokenizer js-tokenizer.c -lrex -lrlib
so in short :
export LD_LIBRARY_PATH=/usr/include/rpatk:/usr/local/lib:/lib:/usr/lib
then running the configure may fix the issue
where the bit in bold would the path to the includes for this project