Trouble with header file - c

I got the following error on compiling a c code I wrote. I understand that the problem is in the header file. Can anyone please tell me which all header files are needed to define these functions.
sign.c: In function ‘main’:
sign.c:78: warning: assignment makes pointer from integer without a cast
/tmp/ccnsSeHy.o: In function `sign_data_evp':
sign.c:(.text+0x68): undefined reference to `check_ssl_rv'
sign.c:(.text+0xd5): undefined reference to `check_ssl_rv'
sign.c:(.text+0x13e): undefined reference to `check_ssl_rv'
/tmp/ccnsSeHy.o: In function `main':
sign.c:(.text+0x1ca): undefined reference to `initialize'
sign.c:(.text+0x1d6): undefined reference to `select_engine'
sign.c:(.text+0x20a): undefined reference to `sign_data'
sign.c:(.text+0x216): undefined reference to `clean_engine'
sign.c:(.text+0x21b): undefined reference to `clean_up'
collect2: ld returned 1 exit status
The header files that I have used so far is:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#ifdef __VMS
#include <socket.h>
#include <inet.h>
#include <in.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
Operating platform: Linux
Thanks in advance.

You understand it wrong. It does not complain about unknown function prototype, it complains during the linking. So you probably forgot to link with some library or some object file.

try with this
gcc sslprogname.c -o sslprogname -Wl,-Bstatic -lssl -Wl,-Bdynamic -lssl3 -lcrypto.It worked for me

Related

undefined reference to xxxxxxx, linking issue stm32cube

I am unable to compile my code successfully due to undefined references.
I am using the STM32Cube IDE compiler which is based of atollic true studio.
The undefined references are due to headers not linking.
I have included the headers into my main program and have included them in the include path for the compiler.
#include "base64_codec_transport.h"
#include "line_buffer_transport.h"
#include "modem_ussd_transport.h"
#include "thingstream_transport.h"
#include "client_api.h"
#include "client_platform.h"
#include "client_set_callback.h"
#include "custom_modem_transport.h"
#include "debug_printf_core.h"
#include "log_client_transport.h"
#include "log_modem_transport.h"
#include "log_protocol_transport.h"
#include "log_transport.h"
#include "modem2_transport.h"
#include "modem_set_callback.h"
#include "modem_transport.h"
#include "predefined_topics.h"
#include "quectel_modem2_config.h"
#include "ring_buffer_transport.h"
#include "sdk_data.h"
#include "serial_transport.h"
#include "sim7600_modem2_config.h"
#include "transport_api.h"
#include "ublox_modem2_config.h"
The consol errors are below:
All these functions are defined in their respective headers as follows, to give one example.
#define line_buffer_transport_create Thingstream_createLineBufferTransport
which then points to
extern ThingstreamTransportResult Thingstream_line_buffer_deliver(ThingstreamTransport *self);
I have also rebuilt the index multiple times and still no success.
I don't know what else to do to solve this.

can't compile c program that uses openssl libraries

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
)

Libnetlink, gcc. undefined reference to rtnl_open

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>

Error including netinet/in.h

Even after trying all orders of header file inclusion,
I still get the error for netinet/in.h
/usr/include/netinet/in.h:34: error: expected identifier before numeric constant
I have included the following header files
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/in.h>
How do I get rid of this error?
I compile with gcc -g3 -Wall.
netinet/in.h doesn't have header guard so what's happening is some variable is already been defined in netinet/ip.h header file. try pushing netinet/in.h to beginning of the file.

popen implicitly declared even though #include <stdio.h> is added

This is tiny snippet of my code.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
...
FILE * pipe;
...
pipe = popen ("ls /tmp -1", "r");
...
pclose(pipe);
blarg.c:106: warning: implicit declaration of function ‘popen’
blarg.c:106: warning: assignment makes pointer from integer without a cast
blarg.c:112: warning: implicit declaration of function ‘pclose’
blarg.c:118: warning: assignment makes pointer from integer without a cast
I'm really unsure. I looked up popen and all it requires is stdio.h which is provided. What is missing, or is the problem in the rest of my code (I don't really want to show more code because its an a assignment).
As the man page says:
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE
|| _SVID_SOURCE
So you should #define _BSD_SOURCE or one of the others before #includeing stdio.h.
Replace -std=c99 or -std=c11 etc with -std=gnu99 or -std=gnu11.
I put the prototypes of popen and pclose at the top of my code. It seemed to have settled the problem.

Resources