Shared library compilation gcc - c

I am trying to compile my program that uses the libwebsockets library.
I have downloaded and compiled the libwebsockets on my linux machine and has following directory structure.
/libwebsockets/build/bin/ -> contains the compiled binaries (test-server,test-client etc)
/libwebsockets/build/lib/ -> contains the shared libraries. libwebsockets.so libwebsockets.a libwebsockets.so.5.0.0
/libwebsockets/lib/ -> contains all the header files. eg: libwebsockets.h libwebsockets.c
/libwebsockets/test-server -> contains application programs..(test-server.c test-client.c etc)
Now I created my C program file simple-server.c in
/libwebsockets/test-server directory.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#include <libwebsockets.h>
when I try to compile this program:
gcc -L/home/gadre/Desktop/Testing/libwebsockets/build/lib/ -lwebsockets simple-server.c -o s
I get linker error:
simple-server.c:(.text+0x14c): undefined reference to `lwsl_emit_syslog'
simple-server.c:(.text+0x158): undefined reference to `lws_set_log_level'
simple-server.c:(.text+0x16d): undefined reference to `libwebsocket_get_internal_extensions'
simple-server.c:(.text+0x1a5): undefined reference to `libwebsocket_create_context'
simple-server.c:(.text+0x1c4): undefined reference to `_lws_log'
simple-server.c:(.text+0x1e9): undefined reference to `libwebsocket_service'
simple-server.c:(.text+0x209): undefined reference to `libwebsocket_context_destroy'
simple-server.c:(.text+0x21d): undefined reference to `_lws_log'
How to solve this..I tried putting the path in the LD_LIBRARY_PATH as well. but still linker cannot find the shared libraries.

I am able to compile for now... I used following command. with run time link option.
gcc -L/home/gadre/Desktop/Testing/libwebsockets/build/lib -Wl,-rpath=/home/gadre/Desktop/Testing/libwebsockets/build/lib -Wall -o server incr_server.c -lwebsockets
I don't know specifics but it works for now

Related

Using vscode, why can't I compile when both "iosteam" and "stdio.h" are included?

When I declare and at the same time, vscode can't complie the file and there is the error log:
> Executing task: C:\mingw64\bin\gcc.exe -g d:\CODES\C++\try\main.cpp -o d:\CODES\C++\try\main.exe <
C:\Users\16337\AppData\Local\Temp\ccqDR0fO.o: In function `__tcf_0':
C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
C:\Users\16337\AppData\Local\Temp\ccqDR0fO.o: In function `__static_initialization_and_destruction_0':
C:/mingw64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/iostream:74: undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
My code is very simple:
#include <iostream>
#include <stdio.h>
int main()
{
printf("print something");
return 0;
}
Dev-C++ can compile that code properly.
If I delete #include <iostream>, it can be properly compiled.
<iostream> is a standard C++ header, so you need a C++ compiler and linker to use it. gcc is smart enough to compile .cpp files as C++, however it won't link the standard library into your program, hence the undefined references.
You need to use g++, which will link the standard C++ library by default, as Dev-C++ does.

Compile non-mpi libraries with mpicc

I want to compile mpi with my own libraries. And I'm not sure if the options that work on gcc such as -I/ -L/ are available for mpicc.
I'm trying to compile with the following options but I get the following error.
mpicc -I$(CURRENT_DIR)/util -I$(CURRENT_DIR) -L$(CURRENT_DIR)/util -o server server.c
mpicc -I./util -I. -L./util -o server server.c
/tmp/ccA5be6Z.o: En la funciĆ³n `main':
server.c:(.text+0x195): undefined reference to `list_create'
server.c:(.text+0x219): undefined reference to `list_add'
server.c:(.text+0x228): undefined reference to `list_count'
collect2: error: ld returned 1 exit status
Those are my server.c includes
#include <mpi.h>
#include <list.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
And this is my pwd/util folder
$ ls
list.c list.h list.o
...
mpicc (and the other MPI build commands) are only wrappers on top of your compilers. So if you are using gcc, all the gcc options and directives are available.
Concerning OpenMPI, you can exhibit the compiler and the options used by using the --showme option. You can see the details in the OpenMPI FAQ (https://www.open-mpi.org/faq/?category=mpi-apps#wrapper-showme-with-no-file). Similar options are available in MPICH (https://www.mpich.org/static/docs/v3.2.x/www1/mpicc.html)
Your issue here is not related to MPI. You are just missing adding some objects (probably list.o) in the linking step. You should consider creating a Makefile to ease the build process.

Difficulty in compiling code on OSX terminal that works in Linux environment

I'm taking some working code (that I didn't write and the creator is not around anymore) to try and get it to compile on my Mac (10.9.5) using terminal. I'm getting the following error when compiling (gcc -o code code.c):
Undefined symbols for architecture x86_64:
"_add_history", referenced from:
_rl_gets in talk-c856c6.o
"_read_history", referenced from:
_main in talk-c856c6.o
"_readline", referenced from:
_rl_gets in talk-c856c6.o
"_write_history", referenced from:
_rl_gets in talk-c856c6.o
ld: symbol(s) not found for architecture x86_64
here are all my includes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <strings.h>
#include <readline/readline.h>
#include <readline/history.h>
I've found here and elsewhere online similar issues and have tried
#include <readline.h>
but get fatal error: 'readline.h' file not found
(same for #include <libedit.dylib>)
I'm a bit of a novice with external libraries so maybe I need to install another one? Is my issue just that the mac needs an additional library installed to use those 5 functions creating errors, or are one of my #includes only for Linux and not for Mac? I thought since both the Mac and the Linux are using Unix it would be the same, but apparently there are subtleties I don't understand.
Thanks in advance.
Did you bass the correct flags to the compiler? Seems like you are not linking against the libedit. Try passing -ledit to the compiler and tell us if that helps.
#include <libedit.dylib>
This is never correct. You can't #include a library, you need to tell the compiler that you want to link against this library when invoking it.

Not able to link dependent header files in cpp file for Rcpp

Based on this tutorial - http://www.r-bloggers.com/using-r-callling-c-code-with-rcpp/
I was trying to call a C function from R.
The C code have following dependencies and it works perfectly after compiling the C code
#include <json/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <time.h>
#include <math.h>
#include <Rcpp.h>
Now when i am trying to load the so file , i am seeing the below error.
dyn.load("storage.so")
Error in dyn.load("storage.so") :
unable to load shared object '/home/algotree/Rcode/storage.so':
/home/algotree/Rcode/storage.so: undefined symbol: json_object_array_length
Seems R is not able to link the rest of header files.
How can I fix it?
This has nothing to do with Rcpp (for which we also provide ample documentation regarding use on its own, in package, via inline, ...).
You seem to use JSON-parsing functionality, but apparently have not linked to a JSON-parser library corresponding to the header json/json.h you included.
Apart from this question being incomplete in its code example and hence not reproducible, I see two issues here:
learn the ropes about C/C++ program using libraries, and
apply this to the R context.
As you using JSON and Curl based on your headers, you could (and probably should) study the corresponding packages like RJSONIO and RCurl.
If you know what is going there and understand the mechanics, you can then use Rcpp to provide the new functionality you are seeking. But just by throwing Rcpp in the mix, these issues do not address themselves. You need to understand how in include headers and link libraries.
Here is how i solved the issue , for running the code i had to add -lcurl and -ljson as command link arguement. So the command R CMD SHLIB should have executed is the below commands
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/include/ -fpic -O3 -pipe -g -c storage.cpp -o storage.o
g++ -shared -o storage.so storage.o -L/usr/lib -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lcurl -ljson -lR
This can be done by editing the PKG_LIBS flags.

GCC: How can I make this compiling and linking work?

I want to use from the file tester-1.c functions that I defined in libdrm.h and gave implementation in libdrm.c. The three files are on the same folder and use pthread functions.
Their include files are:
libdrm.h
#ifndef __LIBDRM_H__
#define __LIBDRM_H__
#include <pthread.h>
#endif
libdrm.c <- has no main()
#include <stdio.h>
#include <pthread.h>
#include "libdrm.h"
tester-1.c <- has teh main()
#include <stdio.h>
#include <pthread.h>
#include "libdrm.h"
The compiler error for libdrm.c says:
gcc libdrm.c -o libdrm -l pthread
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
And the compiler errors for tester-1.c say:
gcc tester-1.c -o tester1 -l pthread
/tmp/ccMD91zU.o: In function `thread_1':
tester-1.c:(.text+0x12): undefined reference to `drm_lock'
tester-1.c:(.text+0x2b): undefined reference to `drm_lock'
tester-1.c:(.text+0x35): undefined reference to `drm_unlock'
tester-1.c:(.text+0x3f): undefined reference to `drm_unlock'
/tmp/ccMD91zU.o: In function `thread_2':
tester-1.c:(.text+0x57): undefined reference to `drm_lock'
tester-1.c:(.text+0x70): undefined reference to `drm_lock'
tester-1.c:(.text+0x7a): undefined reference to `drm_unlock'
tester-1.c:(.text+0x84): undefined reference to `drm_unlock'
/tmp/ccMD91zU.o: In function `main':
tester-1.c:(.text+0x98): undefined reference to `drm_setmode'
tester-1.c:(.text+0xa2): undefined reference to `drm_init'
tester-1.c:(.text+0xac): undefined reference to `drm_init'
tester-1.c:(.text+0x10e): undefined reference to `drm_destroy'
tester-1.c:(.text+0x118): undefined reference to `drm_destroy'
All these functions are defined in libdrm.c
What gcc commands should I use to make these files compile and link?
To compile your .c sources to object files, use GCC's -c option. Then you can link the object files to an executable, against the required library:
gcc libdrm.c -c
gcc tester-1.c -c
gcc tester-1.o libdrm.o -o tester1 -lpthread
Doing compiling and linking in one go, as many others have suggested, works fine too. However, it's good to understand that the build process involves both of these stages.
Your build failed, because your translation modules (=source files) required symbols from each other.
libdrm.c alone couldn't produce an executable, because it does not have a main() function.
tester-1.c's linking failed, because the linker wasn't informed about the required symbols defined in libdrm.c.
With -c option, GCC compiles and assembles the source, but skips linking, leaving you with .o files, which can be linked into an executable or packaged into a library.
gcc tester-1.c libdrm.c -o tester1 -l pthread
You need to compile all the source files in one go rather than doing them individually. Either that or compile libdrm.c as a library and then link it with tester1.c when you compile that.
gcc test-1.c libdrm.c -o libdrm -l pthread

Resources