Linking C-Library libcypher-parser to golang project - go

So I am trying to link the libcypher-parser library to my local golang project but so far, when i'm trying to build my code I am getting ./cypher-test.go:22:35: could not determine kind of name for C.cypher_parse. I'm not making progress with this, I hope someone can help me out with this.
I am using Linux and I installed the library as instructed:
$ git clone https://github.com/cleishm/libcypher-parser.git
$ cd libcypher-parser
$ ./autogen.sh
$ ./configure
$ make clean check
$ sudo make install
So far so good.
libcypher-parser.a
libcypher-parser.so
libcypher-parser.so.11.0.0
libcypher-parser.la
libcypher-parser.so.11
pkgconfig/
can now be found in /usr/local/lib and
cypher-parser.h
can be found in /usr/local/include
As far as I understand it from their README.md i should be able to link the installed library and obtain the required flags via pkg-config --libs libcypher-parser. This was complaining about a missing PKG_CONFIG_PATH so i set it to export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig. Now pkg-config --libs libcypher-parser gave me the following:
-L/usr/local/lib -lcypher-parser
Furthermore i set LD_LIBRARY_PATH=/usr/local/lib
When I run the following code:
package main
// #cgo CFLAGS: -I/usr/local/include
// #cgo LDFLAGS: -L/usr/local/lib -lcypher-parser -Wl,-rpath=/usr/local/lib
// #include <cypher-parser.h>
// #include <errno.h>
// #include <stdio.h>
import "C"
import (
"fmt"
)
func main() {
options := C.CYPHER_PARSE_ONLY_STATEMENTS
fmt.Println(options)
cypher_parse_result_t * result = C.cypher_parse("MATCH (n) RETURN n", nil, nil, options)
}
it gives me ./cypher-test.go:22:35: could not determine kind of name for C.cypher_parse
I tried many different paths and combinations for the CFLAG and LDFLAGS but i wasn't able to link the library/use the function. It's always the same outcome. Just some examples I've tried:
// #cgo CFLAGS: -I/usr/local/include
// #cgo LDFLAGS: -L/usr/local/lib -lcypher-parser -Wl,-rpath=/usr/local/lib
// #cgo CFLAGS: -I/usr/local/lib
// #cgo LDFLAGS: -L/usr/local/lib -lcypher-parser -Wl,-rpath=/usr/local/lib
// #cgo CFLAGS: -I../libcypher-parser/lib/src
// #cgo LDFLAGS: -L../libcypher-parser/lib/ -lcypher-parser -Wl,-rpath=../libcypher-parser/lib
(The last one was a desperate try to link inside the checked out repository)
Does anyone have an idea what I might be missing ? It's my first time trying to link a c-library in go - i hope the catch is not too obvious :)

Related

Link only what is used/needed with Clang on MacOS

On MacOS, the dynamical linking behaviour seems to be fundamentally differnt from that of *Nix.
The problem is that on MacOS, Clang adds whatever libraries given at linking time to the produced binary, regardless of whether their symbols are needed or not.
Minimal C-example:
main.c
#include <stdio.h>
int main(void)
{
puts("Hello World!");
return 0;
}
foo.c
int foo(void)
{
return 42;
}
Compiling on MacOS with
clang -dynamiclib -o libfoo.dylib foo.c
clang -o main main.c -L. -lfoo
and then, using otool -L main, one finds that main binary depends on libfoo.dylib, althought it does not need the symbols in libfoo.dylib at all.
Under Linux, the result is different: Using
gcc -shared -fPIC -o libfoo.so foo.c
gcc -o main -L. -lfoo
to compile and then, ldd main shows no dependence on libfoo.so.
Tested with Apple Clang 12.0.0 and gcc 10.2.1 (Debian 10.2.1-6).
How could one reproduce the behaviour on Linux (“Link what you use/need”), when compiling and linking on MacOS?

omp.h' file not found while compiling source code

I am trying to compile this this source code (https://sites.google.com/site/bgcsoftware/) on a mac. I installed both hdf5 and gsl using homebrew.
Would you know what the problem might be?
Thank you in advance!
h5c++ -Wall -O2 -o bgc bgc_main.C bgc_func_readdata.C bgc_func_initialize.C bgc_func_mcmc.C bgc_func_write.C bgc_func_linkage.C bgc_func_ngs.C bgc_func_hdf5.C mvrandist.c -lgsl -lm
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
bgc_main.C:17:10: fatal error: 'omp.h' file not found
#include <omp.h>
^~~~~~~
1 error generated.
bgc_func_mcmc.C:12:10: fatal error: 'omp.h' file not found
#include <omp.h>
^~~~~~~
1 error generated.
It looks like clang is the actual compiler being used. When compiling OpenMP with clang you need to pass the -fopenmp flag.
Try adding the -fopenmp flag like this:
h5c++ -fopenmp -Wall -O2 -o bgc \
bgc_main.C bgc_func_readdata.C bgc_func_initialize.C \
bgc_func_mcmc.C bgc_func_write.C bgc_func_linkage.C \
bgc_func_ngs.C bgc_func_hdf5.C mvrandist.c -lgsl -lm
The -fopenmp flag tells the compiler replace the code marked with #pragma omp ... with generated parallel code and should automatically add the correct -I include flags behind the scenes.
You should be able to run
h5c++ --help | grep openmp
To see other openmp related flags, depending on your compiler/OS.
adding -fopenmp did not help. However, the original code did run when I installed:
brew install --build-from-source libomp

I'm having an issue linking the standard crt when using clang in linux with c++

I'm working on porting a windows project to linux so I am trying to learn/setup my build pipeline on linux(ubuntu). I use clang on windows and would like to use clang/llvm to compile my code on ubuntu but I am having an issue linking the c runtime library to my code. Here's an example of what I am trying to run just so I can set up the build scripts,
// -- system includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// -- my includes
#include "crt.h"
int main() {
char title[] = "Text";
int title_length = strlen(title);
// -- sending string and length to platform dependent code
CreateBox(title, title_length);
return(0); }
After installing clang 3.4 on Ubuntu 14.04 LTS I run the above code as well as platform dependent code from the terminal,
clang++ -c main.cpp box_linux.cpp
The file compiles without error. Let's say I had I use the following command when trying to link,
ld main.o box.o
I get back the following error every single time,
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
In function 'main':
(.text+0x30): undefined reference to 'strlen'
I try linking libc++, libstdc++ but nothing seems to work. I am admittedly very, very new to linux and clang on linux so I apologize in advance. I have checked different questions here on stackoveflow as well as ubuntu's forums and LLVM's forums/docs to no avail. If anyone could point me in the right direction it would be greatly appreciated.
If you are using clang (or gcc) on linux you don't have to do the linking on your own. You can let clang do the linking for you and it will select the necessary libraries for your system just remove the -c flag form command line or use clang main.o. Clang understands that a .o file is already an object file and passes it on to the linker.
If you realy want to invoke ld on your own then start with clang++ -v main.cpp. The -v switch let clang print the invocation command for ld which e.g., looks like this:
/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. -L/data/home/user/bin/../lib -L/lib -L/usr/lib /tmp/test-574b88.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
on my Ubuntu 14.04 LTS.

Compiling libJPEG with Alchemy (-swc)

I have tried for a few days to compile -swc with -ljpeg but have not had any luck.
Here is what i have been doing.
in my jpeg_sample_linker.c, it has two headers
#include "AS3.h"
#include <jpeglib.h> //or #include "jpeglib.h"
and the rest of the file is pretty much empty but compilable(because all i want to do is to compile the file with the headers first).
And my gcc command is:
alc-on
gcc -swc -ljpeg jpeg_sample_linker.c -o jpeg_sample_linker.swc
alc-off
And, gcc keeps on saying it cannot locate libjpeg.h
i have also tried
alc-on
gcc -swc -I/usr/local/include -L/usr/local/lib -ljpeg jpeg_sample_linker.c -o jpeg_sample_linker.swc
alc-off
It still reported the same error "error: jpeglib.h: No such file or directory".
May i know how i could fix this compilation error?
Billion thanks
G
What a bummmer,
the compilation can be solved by editing gcc in /Library/alchemy-darwin-v0.5a/achacks
or
moving everything in /usr/local/lib and /usr/local/include to
to $ALCHEMY_HOME/usr/local/include and $ALCHEMY_HOME/usr/local/lib
!

Makefile for a simple application

I have a file app.c which uses two libraries GStreamer and libXml2. To compile the application I type the following on Terminal
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) app.c -o app -I/usr/include/libxml2 -lxml2
When I try to Makefile with the contents as follows :
all:
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) app.c -o app -I/usr/include/libxml2 -lxml2
run:
./app
clean:
rm app
On running make command I get the errors as expected. What is the significance of
$(pkg-config --cflags --libs gstreamer-0.10)
on Echoing the above I get some files which when included in Makefile gives me the correct output.
pkg-config --cflags libraryX outputs the path to the header files of libraryX. Without this, the compiler does not know where to look for the header files, and compilation will fail.
Similarly, pkg-config --libs libraryX outputs the path to the actual compiled library files of libraryX. Without this, the linker does not know where to look for the library files, and linking will fail.
pkg-config --cflags --libs libraryX is just combining what I described above. Since you're using gcc to do both compilation and linking, you just pass those parameters together to gcc.

Resources