I want to use the libnetlink library shipped with iproute2. I specified it's include dir with -I and it's lib dir with -L. However I still get some undefined reference errors.
test.c: In function ‘addqdisc’:
test.c:26:3: warning: ignoring return value of ‘rtnl_open’, declared with attribute warn_unused_result [-Wunused-result]
26 | rtnl_open(rth, 0);
| ^~~~~~~~~~~~~~~~~
test.c:27:3: warning: ignoring return value of ‘rtnl_talk’, declared with attribute warn_unused_result [-Wunused-result]
27 | rtnl_talk(rth, &req.n, NULL);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccCnXM9F.o: in function `addqdisc':
test.c:(.text+0x79): undefined reference to `rtnl_open'
/usr/bin/ld: test.c:(.text+0x97): undefined reference to `rtnl_talk'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <libnetlink.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int addqdisc() {
struct rtnl_handle *rth;
struct {
struct nlmsghdr n;
struct tcmsg t;
char buf[64*1024];
} req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE,
.n.nlmsg_type = RTM_NEWQDISC,
.t.tcm_family = AF_UNSPEC,
};
rtnl_open(rth, 0);
rtnl_talk(rth, &req.n, NULL);
}
int main() {
addqdisc();
return 0;
}
gcc test.c -o test -I/pwd/Downloads/linux-5.17/iproute2/include -l /pwd/Downloads/linux-5.17/iproute2/lib/libnetlink.a -lnetlink -static
Anyone knows why?
Related
I used search.h for my c program, where I need to put #define _GNU_SOURCE to the first line in order to introduce multiple hash tables. But after that, errors like undefined reference to 'log10' and undefined reference to 'PQntuples' popped up. I certainly need all the packages there, how should I now compile the program? Any help would be deeply appreciated! Thanks.
The headers:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
// library for psql
#include <libpq-fe.h>
#include <unistd.h>
#include <time.h>
#include <search.h>
int main(void){
char host[] = "localhost";
char port[] = "5432";
char db_name[] = "db_name";
char user[] = "test_usr";
char password[] = "123456";
sprintf(db_str, "host=%s port=%s dbname=%s user=%s password=%s",
host, port, db_name, user, password);
PGconn *db_connection = DBConnect(db_str);
struct hsearch_data htab;
hcreate_r(10, &htb);
ENTRY e, *ep;
e.key = "test";
e.data = (void *) 1;
hsearch_r(e, ENTER, &ep, &htab);
}
And this was how I compile the file:
gcc -Wall -Wextra -I/home/userX/postgresql/include -L/home/userX/postgresql/lib -lm -lpq -g my_program.c
Specifiy the libraries at the end of the command line
gcc -Wall -Wextra -I/home/userX/postgresql/include \
-L/home/userX/postgresql/lib -g my_program.c -lpq -lm
// ^^^^^^^^
gcc looks at required symbols left to right on the command line.
With the libraries before the source files, when gcc processes the -llib argument it does not have any requirements and therefore does not "extract" any function from the library.
The problem is I need to use LD_PRELOAD to modify the opendir() function in that is part of the ls command to restrict the opening of directories when the target path is not within the /home directory. The error I am getting is with the return line that says:
return((*original_opendir)(_name));
In that the original_opendir is bolded and it says error: invalid use of incomplete typedef 'DIR{aka struct _distream)'
I have attached my code below. Please let me know if you have any ideas I would really appreciate it![enter image description here][1]
The file name was called modlib3.c and I compiled it with this when I got the errors:
//gcc -o modlib3.so -shared -fPIC -D_GNU_SOURCE modlib3.c -ldl
Here is my code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <dlfcn.h>
#include <dlfcn.h>
DIR *opendir(const char *_name) {
DIR (*original_opendir)(const char *);
*(void **)(&original_opendir) = dlsym(RTLD_NEXT, "*opendir");
if (strcmp(_name, "/home") <0 || strcmp(_name, "/home")>0){
syslog(LOG_EMERG, "Cannot open! ");
exit(1);
}
return((*original_opendir)(_name));
}
Fixed some of the problems in the code, please check the differences carefully
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <dirent.h>
#include <dlfcn.h>
DIR *opendir(const char *_name) {
DIR *(*original_opendir)(const char *);
*(void **)(&original_opendir) = dlsym(RTLD_NEXT, "opendir");
if (strncmp(_name, "/home", 5)!=0) {
syslog(LOG_EMERG, "Cannot open!");
exit(1);
}
return((*original_opendir)(_name));
}
I plan to use both SDL_GetKeyboardState and SDL_Overlay but it seems there is a conflict.
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
int main()
{
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
SDL_Overlay *bmp;
printf("hello world!");
}
Compile:
gcc -c main.cpp
When order of headers are:
#include <SDL/SDL.h>
#include <SDL2/SDL.h>
error: ‘SDL_GetKeyboardState’ was not declared in this scope
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
^
or
#include <SDL2/SDL.h>
#include <SDL/SDL.h>
error: ‘SDL_Overlay’ was not declared in this scope
SDL_Overlay *bmp;
^
Even adding
#include <SDL2/SDL_video.h>
does not solve the problem.
What header should I add to use SDL_Overlay?
I am having a hard time finding this missing reference when running : gcc server.c -I /pwdmanlib/src -lssl -lcrypto -o server the include is my src files (headers needs etc..) and the rest is th required ssl libraries.
I am getting the following output from gcc:
In file included from server.h:49:0,
from server.c:39:
/pwdmanlib/src/util/constants.h:30:0: warning: "LINE_MAX" redefined
#define LINE_MAX 2048
^
In file included from /usr/include/limits.h:147:0,
from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/limits.h:168,
from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/limits.h:34,
from /pwdmanlib/src/util/constants.h:26,
from server.h:49,
from server.c:39:
/usr/include/x86_64-linux-gnu/bits/posix2_lim.h:81:0: note: this is the location of the previous definition
#define LINE_MAX _POSIX2_LINE_MAX
^
In file included from server.c:39:0:
server.h: In function ‘start_server’:
server.h:126:34: warning: comparison between pointer and integer
if (p == NULL || listen_sock == NULL) {
^
In file included from server.c:39:0:
server.h: In function ‘routeClient’:
server.h:394:29: warning: passing argument 1 of ‘sendall’ makes pointer from integer without a cast [-Wint-conversion]
if (sendall(worker_sock, resp_data, fileLen) == -1) {
^
In file included from server.c:39:0:
server.h:70:5: note: expected ‘SSL * {aka struct ssl_st *}’ but argument is of type ‘int’
int sendall(SSL *ssl, char *buf, ssize_t *len);
^
/tmp/ccubinQD.o: In function `InitSSL':
server.c:(.text+0x1305): undefined reference to `OPENSSL_init_ssl'
server.c:(.text+0x1314): undefined reference to `OPENSSL_init_ssl'
server.c:(.text+0x1323): undefined reference to `OPENSSL_init_crypto'
/tmp/ccubinQD.o: In function `InitCTX':
server.c:(.text+0x1333): undefined reference to `TLS_server_method'
server.c:(.text+0x1350): undefined reference to `SSL_CTX_set_options'
collect2: error: ld returned 1 exit status
I found the OPENSSL_init_ssl function call in the ssl library and it is apparently getting included but can't be found by other references to it in the library?? The includes from my program are specified below:
ssl_funcs.h
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
server.h
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include "util/oop.h"
#include "util/stringops.h"
#include "util/constants.h"
#include "fawkes_proto.h"
#include "crypto/ssl_funcs.h"
server.c
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include "server.h"
#include "util/constants.h"
When linking in dynamic libraries with the -l option, these must occur last, after all other options:
gcc server.c -I /pwdmanlib/src -o server -lssl -lcrypto
Besides this, you should address the warnings in your code. These can potentially lead to undefined behavior.
To add to dbush's answer above I also needed to compile with an explicit target directory for the linking library like so: gcc server.c -I/pwdmanlib/src -o server -L/usr/local/lib -lssl -lcrypto
Moreover, I also implemented a solution for this in CMake (build system I use for my projects) and provided that below as well in case anyway else might find that useful. This is just the pertinent portion of it, if anyone wants the full src to the cmake I would be more than happy to provide it.
CMakeLists.txt
# Add libraries
include_directories(${LOCAL_LIBS_DIR})
include_directories("/usr/local/lib")
#link_directories("/usr/local/lib")
add_library(ssl SHARED IMPORTED) # or STATIC instead of SHARED
set_property(TARGET ssl PROPERTY IMPORTED_LOCATION "/usr/local/lib/libssl.so")
add_library(crypto SHARED IMPORTED) # or STATIC instead of SHARED
set_property(TARGET crypto PROPERTY IMPORTED_LOCATION "/usr/local/lib/libcrypto.so")
#include_directories("/opt/openssl-1.1.0e")
#find_package (my_library COMPONENTS REQUIRED component1 component2 OPTIONAL_COMPONENTS opt_component)
# Define build targets and link libraries
add_executable(main ${SOURCE_FILES})
target_include_directories(main PUBLIC /usr/include/openssl)
target_link_libraries(main
PRIVATE ${Boost_LIBRARIES}
PRIVATE ${PostgreSQL_LIBRARIES}
PRIVATE ${cJSON_ROOT_DIR}
# PRIVATE ${CryptoPP_ROOT_DIR}
# PRIVATE ${Kore_ROOT_DIR}
# PRIVATE ${POCO_LIBRARIES}
PRIVATE ssl
PRIVATE crypto
)
I have a problem with using libnetlink.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libnetlink.h>
int main(int argc, char **argv)
{
struct rtnl_handle* rth;
unsigned bitmap_socket_group;
if ((rtnl_open(rth,bitmap_socket_group))==-1)
{
printf("Some sht happend\n");
return -1;
}
return 0;
}
After that i'm running gcc -o rt route-test.c and it returns:
/tmp/ccvqPhGI.o: In function `main': route-test.c:(.text+0x19): undefined reference to `rtnl_open'
collect2: error: ld returned 1 exit status
All i want is just to test libnetlink. As a helloworld.
Is it a problem with gcc arguments?
Hope for help:)
It appears you are either missing a header or your a failing to link against a needed library. Are you sure there is no -lnetlink you need to include in your compile string?
– David C. Rankin
You're right, i need -lnetlink! Thank you:)
– AlexZ
According to the documentation, you may need to include more librairies :
#include <asm/types.h>
#include <libnetlink.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>