I'm trying to link gsl in a small c program.
#include "stdlib.h"
#include "stdio.h"
#include "gsl/gsl_block_float.h"
#include "gsl/gsl_matrix_float.h"
int main(void)
{
gsl_matrix_float* m = gsl_matrix_float_alloc(2, 2);
gsl_matrix_float_fprintf(stdout, m, "%f");
}
I'm compiling with gcc -lgsl -lgslcblas -lm program.c. I've tried gcc $(pkg-config --cflags gsl) $(pkg-config --libs gsl) program.c as well, along with gsl-config. In every case, gcc returns
/tmp/cc1wKgXm.o: In function `main':
program.c:(.text+0x13): undefined reference to `gsl_matrix_float_alloc'
program.c:(.text+0x32): undefined reference to `gsl_matrix_float_fprintf'
collect2: error: ld returned 1 exit status
objdump --syms /usr/lib/libgsl.so | grep gsl_matrix_float returns the proper symbols, as does grepping my headers. Everything is in /usr/lib or /usr/include What am I doing wrong?
I got this from the ubuntu forums. The order of the arguments togcc might be the issue
gcc -o program program.c `gsl-config --cflags --libs`
Related
I'm trying to compile a piece of code which uses libusb:
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <assert.h>
int main(void) {
libusb_context *context = NULL;
int rc = 0;
rc = libusb_init(&context);
assert(rc == 0);
libusb_exit(context);
return 0;
}
Upon compiling with gcc -lusb -lusb-1.0 sample.c -o sample the following errors emerge:
/tmp/ccr65JBT.o: In function `main':
sample.c:(.text+0x2e): undefined reference to `libusb_init'
sample.c:(.text+0x62): undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status
To make sure libusb is availible on my system:
raven#enforcer:~/sample$ pkg-config --libs libusb-1.0
-lusb-1.0
raven#enforcer:~/sample$ pkg-config --libs libusb
-lusb
I'm running Ubuntu 18.04 with gcc 7.3.0-16ubuntu3, how to fix?
Got it working by appending the flags after the .c: gcc sample.c -o sample -lusb -lusb-1.0
Autotools: sample_LDADD instead of sample_LDFLAGS
Thanks to Felix Palmen.
I made a shared library as the follow:
gcc -c output.c
gcc -shared -fPIC -o liboutput.so output.o
When output.c is the follow, it could work.
//#include "output.h"
#include <stdio.h>
int output(const char* st) {
return 1+2;
}
But, when output.c changed as the follow, a error occur.
//#include "output.h"
#include <stdio.h>
int output(const char* st) {
printf("%s\n", st);
return 1+2;
}
This is error message:
/usr/bin/ld: output.o: relocation R_X86_64_PC32 against undefined 符号 `puts##GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: 错误的值
collect2: error: ld returned 1 exit status
I want to know why and how to deal it. Thanks in advance.
You need to compile output.c as position independent code.
gcc -c -fPIC output.c
In the first version you have not called any library function. But in second one printf is being called. In general, compile all sources with -fPIC if you intend to build a shared library later.
I'm trying to build a simple example using libcheck but it fails to build while using the flags reported by pkg-config.
My file: tests/test.c
#include <stdlib.h>
#include <check.h>
START_TEST(zero)
{
int z = 0;
ck_assert_int_eq(0, z);
}
END_TEST
Suite* suite_zero(void)
{
Suite* s;
TCase* tc;
s = suite_create("suite_zero");
tc = tcase_create("zero");
tcase_add_test(tc, zero);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
int number_failed;
SRunner* sr;
Suite* s_zero = suite_zero();
sr = srunner_create(s_zero);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
My system:
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
$ pkg-config --version
0.29.1
When I try to build my test using the flags and libs reported by pkg-config:
$ pkg-config --cflags --libs check
-pthread -lcheck_pic -pthread -lrt -lm -lsubunit
$ gcc -pthread -lcheck_pic -pthread -lrt -lm -lsubunit -g tests/test.c -o tests/zero
/tmp/ccRV2kLw.o: In function `zero':
Code/tests/test.c:4: undefined reference to `tcase_fn_start'
/Code/tests/test.c:7: undefined reference to `_mark_point'
/Code/tests/test.c:7: undefined reference to `_ck_assert_failed'
/tmp/ccRV2kLw.o: In function `suite_zero':
/Code/tests/test.c:15: undefined reference to `suite_create'
/Code/tests/test.c:16: undefined reference to `tcase_create'
/Code/tests/test.c:17: undefined reference to `_tcase_add_test'
/Code/tests/test.c:19: undefined reference to `suite_add_tcase'
/tmp/ccRV2kLw.o: In function `main':
/Code/tests/test.c:30: undefined reference to `srunner_create'
/Code/tests/test.c:32: undefined reference to `srunner_run_all'
/Code/tests/test.c:33: undefined reference to `srunner_ntests_failed'
/Code/tests/test.c:34: undefined reference to `srunner_free'
collect2: error: ld returned 1 exit status
Libcheck was installed by apt and the library and headers are in the typical locations, /usr/lib and /usr/include respectively. Yet it appears it can't be found. I'm a bit stumped as why. Any ideas?
Thanks.
Try adding the flags reported by pkg-config after the compilation command as this answer and the documentation for pkg-config suggests. The compiler needs to know what file it's trying to link the library to beforehand so that the flags can be applied appropriately.
gcc -g test.c -o test -pthread -lcheck_pic -pthread -lrt -lm -lsubunit
or more succinctly
gcc -g test.c -o test `pkg-config --cflags --libs check`
I made a library, and I'm trying to make a test client for it to test my Debian packages. This test is being done on Ubuntu 14.04.
I installed the binary and the developer files and their dependencies.
Here's the source for my test program:
#include <stdio.h>
#include <cquel.h>
int main(int argc, char *argv[])
{
cq_init(1024, 128);
struct dbconn mydb = cq_new_connection(u8"pattstest.delwink.com", u8"patts",
u8"patts", u8"pattsdb");
struct dlist *users;
int rc = cq_select_all(mydb, u8"User", &users, u8"");
if (rc)
return 2;
for (size_t i = 0; i < users->fieldc; ++i)
printf("%s\n", users->fieldnames[i]);
cq_free_dlist(users);
return 0;
}
The program is supposed to connect to a test server and get the column headers from the database (no, that server is not production and does not need to be particularly secure).
I attempted to compile using gcc:
$ gcc -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c
/tmp/ccjd21kP.o: In function `main':
/home/mac/c/main.c:6: undefined reference to `cq_init'
/home/mac/c/main.c:8: undefined reference to `cq_new_connection'
/home/mac/c/main.c:12: undefined reference to `cq_select_all'
/home/mac/c/main.c:19: undefined reference to `cq_free_dlist'
collect2: error: ld returned 1 exit status
I knew something was up, so I attempted the same with clang:
$ clang -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c
Clang compiled just fine! I ran my a.out binary, and it printed the column headers as expected.
Why did gcc fail to link to my library?
EDIT: I thought to check my LD_LIBRARY_PATH to find that it was blank. However, setting it to /usr/lib/x86_64-linux-gnu (which is the location of my shared object) did not change the behavior of gcc.
Order of arguments matter a lot for gcc; you should use
gcc -Wall $(pkg-config --cflags cquel) $(mysql_config --cflags) \
-std=c11 main.c \
$(pkg-config --libs cquel) $(mysql_config --libs)
See also this & that.
I try to write simple mongo c client. Source file (a.c):
#include <stdio.h>
#define MONGO_HAVE_STDINT
#include <mongo.h>
void mongo_init_c(mongo *con)
{
mongo_init(con);
}
int main() {
return 0;
}
And i try to compile it with:
gcc -I/usr/local/include -L/usr/local/lib -lmongoc a.c
But get an error:
a.c:(.text+0xd): undefined reference to `mongo_init'
Files /usr/local/include/mongo.h and /usr/local/lib/libmongoc.so exists
How can I correctly compile a.c?
p.s. mongo-2.0.4, gcc-4.6, mongo-c-driver - pulled from github
update
$ nm /usr/local/lib/libmongoc.so | grep init
000034e0 T _init
0000dd10 T bson_init
0000c740 T bson_init_data
0000c7b0 T bson_init_finished_data
0000dc10 T bson_init_size
0000d060 T bson_iterator_init
0000a5e0 T gridfile_init
00009af0 T gridfile_writer_init
000095e0 T gridfs_init
00010a18 R initialBufferSize
00005f40 T mongo_cursor_init
00008da0 T mongo_env_sock_init
00005d90 T mongo_init
000057b0 T mongo_init_sockets
00004800 T mongo_md5_init
00005e40 T mongo_replica_set_init
00005f00 T mongo_replset_init
00005b80 T mongo_write_concern_init
$ gcc -I/usr/local/include -L/usr/local/lib -Wall -Werror -lmongoc a.c
/tmp/cccuNEp1.o: In function `mongo_init_c':
a.c:(.text+0xd): undefined reference to `mongo_init'
Try linking the library after the source file, like gcc a.c -lmongoc. This is because you're using a traditional single-pass linker, which expects to satisfy dependencies with subsequent, not previous, objects specified on the command line.