How do I compile simple gearman worker with C? - c

I am trying to compile a simple gearman worker on C. I use mac. Here is the code:
#include <libgearman/gearman.h>
int main(void) {
gearman_worker_st worker;
gearman_worker_create(&worker);
gearman_worker_add_server(&worker, "localhost", 4730);
return 0;
}
When I try to compile it with:
#gcc test.c
Undefined symbols for architecture x86_64:
"_gearman_worker_add_server", referenced from:
_main in ccLUuf8y.o
"_gearman_worker_create", referenced from:
_main in ccLUuf8y.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I know I have to link gcc with gearman but when try:
#gcc test.c -lgearman
ld: library not found for -lgearman
collect2: ld returned 1 exit status
Any ideas?

maybe your need to define the lib path, like
-L/usr/lib/, use your libgearman.a stored path to substitute -L/usr/lib/

maybe your need to define the "include path" and the "lib path",
for example,
the head file "libgearman/gearman.h" in the /usr/local,
the library libgearman.so in the /usr/local/libgearman/lib
the compile command like,
gcc -I/usr/local -L/usr/local/libgearman/lib test.c -lgearman

As stated here, you have to link it with -lgearman
gcc test.c -lgearman

Related

Symbol not found - linking to hdf library

I am trying to use the hdf5-format to store data. Problem is, that I fail to link against the library. I have the following code
#include <H5Cpp.h>
int main(void){
H5::H5File file("test_MatrixRoundTrip_Double.h5", H5F_ACC_TRUNC);
}
and compile it using
gcc -std=c++11 -o main main.cpp -I /usr/local/include/ -L /usr/local/lib/ -lhdf5 -lhdf5_hl
This always returns the error
Undefined symbols for architecture x86_64:
"H5::FileAccPropList::DEFAULT", referenced from:
_main in main-c207d1.o
"H5::FileCreatPropList::DEFAULT", referenced from:
_main in main-c207d1.o
"H5::H5File::H5File(char const*, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)", referenced from:
_main in main-c207d1.o
"H5::H5File::~H5File()", referenced from:
_main in main-c207d1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I installed the hdf5 library on OSX using
brew install homebrew/science/hdf5
What am I doing wrong here?
You are including the HDF5 C++ header file, but only linking the HDF5 C library. Add the line: -lhdf5_cpp to link the C++ shared object and use locate libhdf5_cpp to find it's libpath.

Can gcc -o and -S be used together

Can I use for example
gcc -o -S output.s abs.c
to generate an assembly file with name output.s? It seems like I can't. When I try to do that, I got following error message.
Undefined symbols for architecture x86_64: "_main", referenced from:
implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit
code 1 (use -v to see invocation)
I do not intend to use the linker, just try to examine the assembly code.
-o must be followed by the name of the output file. So, this would work:
gcc -S abc.c -o output.s

How do I include the object files for openssl/md5 library?

I have included the openssl/md5.h in my source.
#include <openssl/md5.h>
This is my (part of) my code:
char *pf_generate_pfdhr_string(int firstfree, int numpages)
{
const int md5_digest_len = 16;
char hash[md5_digest_len];
MD5_CTX md5_ctx;
MD5_Init(&md5_ctx);
int hdr_arr[2] = {firstfree, numpages};
MD5_Update(&md5_ctx, hdr_arr, 2*sizeof(int));
MD5_Final(hash, &md5_ctx);
return hash;
}
And this is my output
ranlib build/libpf.a
cc -g -O2 -Wall -Wextra -Isrc -rdynamic tests/pf_tests.c build/libpf.a -o tests/pf_tests
Undefined symbols for architecture x86_64:
"_MD5_Final", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
"_MD5_Init", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
"_MD5_Update", referenced from:
_pf_generate_pfdhr_string in libpf.a(pf.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [tests/pf_tests] Error 1
I have included the header file so the compiler knows the function exists. However, it seems unable to find the object files for the md5 library. How can I include it in my build?
If I am using Make, what is the best way to do so?
Thank you.
Check the documentation for your platform. It may be -lcrypto -lssl. It may not be. If your platform supports pkg-config, use pkg-config --libs openssl.

Trouble Linking when compiling a MEX file

I am trying to compile a mex file that I wrote in order to run a pre-existing C program in MATLAB. I am getting the following errors:
>> mex src/main.c -Iinclude -I/Users/my_name/mpfr-3.1.1/src/ -I/Users/my_name/gmp-5.0.5 -Lsrc0 -output cpdetect_c
Undefined symbols for architecture x86_64:
"_binomial_main", referenced from:
_cpdetect in main.o
"_gaussian1_main", referenced from:
_cpdetect in main.o
"_gaussianU_main", referenced from:
_cpdetect in main.o
"_mpfr_clears", referenced from:
_cpdetect in main.o
"_mpfr_exp10", referenced from:
_cpdetect in main.o
"_mpfr_inits2", referenced from:
_cpdetect in main.o
"_mpfr_set_d", referenced from:
_cpdetect in main.o
"_poisson_main", referenced from:
_cpdetect in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am confused because these functions are all part of files that I #Include in my code. How do I get the compiler to find them?
I am running MATLAB R2013a on Mac OS X 10.8 with Xcode version 4.6.3.
Thanks for any advice!
UPDATE1:
#Shai: Contents of my src0 folder:
>> ls src0
Makefile gaussian1.o test_gaussian1.c test_tools.c
Makefile.am loadtraj.c test_gaussian1_point_calc.c test_tools.o
Makefile.in loadtraj.o test_load tools.c
binomial.c main.c test_load.c tools.o
binomial.o main.o test_load.o
cpdetect poisson.c test_poisson.c
gaussian1.c poisson.o test_tools
This C program (originally meant to run on the command line) came from a friend and already had the src0 folder present. I modified the code files in the 'src' folder when writing the mex function, but I didn't touch the src0 folder.
UPDATE 2:
I realized I was pointing to the wrong location for mpfr. I am now using the following compile command:
>> mex src/main.c src/binomial.c src/gaussian1.c src/gaussianU.c src/poisson.c -Iinclude -I/usr/local/include -lmpfr -output cpdetect_c
and it is giving the following errors:
Undefined symbols for architecture x86_64:
"_mpfr_mul_d", referenced from:
_gaussian1_calc_constant_part in gaussian1.o
_gaussianU_calc_constant_part in gaussianU.o
"_mpfr_printf", referenced from:
_find_gaussian1_change_point in gaussian1.o
_gaussian1_point_calc in gaussian1.o
_gaussianU_calc_constant_part in gaussianU.o
_find_gaussianU_change_point in gaussianU.o
_find_poisson_change_point in poisson.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
mex: link of ' "cpdetect_c.mexmaci64"' failed.
This is strange. It's not like the compiler can't find any of the mpfr functions - there are lots of others that it seems to be finding without any trouble (and if I deliberately omit the -lmpfr flag, I get a much longer list of undefined symbols). I did confirm that mpfr_mul_d and mpfr_printf are part of the mpfr distribution I installed. Any ideas? Thanks.
The answer was provided by Amro - it is necessary to both point to the location of the library (using the flag -L/usr/local/lib in this case) and to tell the compiler to link to it (using the flag -lmpfr). Thanks also to Shai for help troubleshooting this.

Mac proj.4 compile error

I installed proj.4 library with homebrew on my Mac 10.7 (using gcc-4.2). When trying to compile the following code:
#include <proj_api.h>
int main(void) {
projPJ pj_merc;
pj_merc = pj_init_plus("+proj=merc");
pj_free(pj_merc);
return 0;
}
I'm getting this error:
$ gcc-4.2 test.c
Undefined symbols for architecture x86_64:
"_pj_init_plus", referenced from:
_main in cccf4vey.o
"_pj_free", referenced from:
_main in cccf4vey.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
EDIT:
The library file is 64bit (gcc-4.2 -m32 test.c lead to the same error):
$ file /usr/local/lib/libproj.dylib
/usr/local/lib/libproj.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Any ideas what's wrong?
Thank you!
You should link against the library.
gcc-4.2 test.c -L/usr/local/lib -lproj
This is what the error is complaining about

Resources