This is a follow up question to Undefined reference to RSA_generate_key in OpenSSL? I have an OpenSSL program to generate an RSA key, as follows :
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
int main(int argc, char* argv[])
{
RSA *r = NULL;
int ret;
r = RSA_new();
BIGNUM *bne = NULL;
ret = RSA_generate_key_ex(r, 2048, bne, NULL);
return 0;
}
When I compile this with
gcc -I../include rsatest.c -lcrypto -L.
I get undefined reference errors to functions in libraries gdi32 and zlib.
So to correct this, I have to add -lz and -lgdi32, as
gcc -I../include/ rsatest.c -lcrypto -lz -lgdi32 -L.
My question is, aren't static libcrypto and libssl self sufficient? As in why am I having to add extra libraries to make this work? Is there an option which I should have added while compiling the library to prevent this?
I'm using a 64 bit Windows with a MinGW compiler. I made the OpenSSL library with the help of MSYS and make.
Related
Here's my code:
#include <openssl/bio.h>
int main (void)
{
BIO* bo = BIO_new(BIO_s_mem());
}
I'm compiling my code by doing gcc -lcrypto test.c.
Any idea what's wrong?
You have the arguments in the wrong order, try gcc test.c -lcrypto
See why order matters
I am trying to build an openssl simple program. Here is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16];
unsigned char out_[16];
strcpy(userKey_,"0123456789123456");
strcpy(in_,"0123456789123456");
fprintf(stdout,"Original message: %s", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s", in_);
return 0;
}
I try to compile it using this command:
gcc -I/home/aleksei/openSSL0.9.8/include -o app -L . -lssl -lcrypto tema1.c
and I get this:
/tmp/ccT1XMid.o: In function `main':
tema1.c:(.text+0x8d): undefined reference to `AES_set_encrypt_key'
tema1.c:(.text+0xa7): undefined reference to `AES_encrypt'
tema1.c:(.text+0xbf): undefined reference to `AES_set_decrypt_key'
tema1.c:(.text+0xd9): undefined reference to `AES_decrypt'
collect2: ld returned 1 exit status
I am under Ubuntu 10.04. How can I get this to work ?
You may be trying to statically link, but the -L option and -lcrypto are looking for a file to link with dynamically. To statically link to a specific library, just specify your .a file on the compiler command line after all your source files.
E.g.,
gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c ./libcrypto.a
For those of you who have this same problem but are using Windows, Mingw and this OpenSSL for Windows (at this time: Win32 OpenSSL v1.0.2a). You need to link to libeay32.a that is located in C:\OpenSSL-Win32\lib\MinGW\ (after installing OpenSSL).
In my case I am using CMake and the powerful CLion IDE, so I had to rename the library to libeay32.dll.a because CMake wasn't locating the library. This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
project(openssl_1_0_2a)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(C:\\OpenSSL-Win32\\include)
set(SOURCE_FILES main.cpp)
link_directories(C:\\OpenSSL-Win32\\lib\\MinGW)
add_executable(openssl_1_0_2a ${SOURCE_FILES})
target_link_libraries(openssl_1_0_2a eay32)
I made the test with this example (which is borrowed from this answer):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"
int main(int argc, char* argv[])
{
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16] = {0};
unsigned char out_[16] = {0};
strcpy((char *) userKey_,"0123456789123456");
strcpy((char *) in_,"0123456789123456");
fprintf(stdout,"Original message: %s\n", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s XXX \n", in_);
return 0;
}
I think the order of the parameter should be reset like follows:
gcc -I/home/aleksei/openSSL0.9.8/include -o app tema1.c -L . -lssl -lcrypto
I want to access the global variable of executable in shared library? I have tried to compile using option -export-dynamic but no luck.
I have tried with extern key word. this also not working.
Any help or suggestion would be appreciable.
Environment c - Linux
executable:-
tst.c
int tstVar = 5;
void main(){
funInso();
printf("tstVar %d", tstVar);
}
lib:-
tstLib.c
extern int tstVar;
void funInso(){
tstVar = 50;
}
Since my code is very big, I just gave the sample which I have used in my program.
It should work. BTW, your tst.cis lacking a #include <stdio.h>. And its main should return an ìnt and end with e.g. return 0;.
With
/* file tst.c */
#include <stdio.h>
int tstVar = 5;
extern void funInso(void);
int main(){
funInso();
printf("tstVar %d\n", tstVar);
return 0;
}
and
/* file tstlib.c */
extern int tstVar;
void funInso(){
tstVar = 50;
}
I compiled with gcc -Wall -c tst.c the first file, I compiled with gcc -Wall -c tstlib.c the second file. I made it a library with
ar r libtst.a tstlib.o
ranlib libtst.a
Then I linked the first file to the library with gcc -Wall tst.o -L. -ltst -o tst
The common practice is to have with your library a header file tstlib.h which would contain e.g.
#ifndef TSTLIB_H_
#define TSTLIB_H_
/* a useful explanation about tstVar. */
extern int tstVar;
/* the role of funInso. */
extern void funInso(void);
#endif /*TSTLIB_H */
and have both tst.c and tstlib.c contain an #include "tstlib.h"
If the library is shared, you should
compile the library file in position independent code mode
gcc -Wall -fpic -c tstlib.c -o tstlib.pic.o
link the library with -shared
gcc -shared tstlib.pic.o -o libtst.so
Note that you can link a shared object with other libraries. You could have appended -lgdbm to that command, if your tstlib.c is e.g. calling gdbm_open hence including <gdbm.h>. This is one of the many features shared libraries give you that static libraries don't.
link the executable with -rdynamic
gcc -rdynamic tst.o -L. -ltst -o tst
Please take time to read the Program Library Howto
your tstVar variable could be defined in the lib. and you can share this variable via functions:
setFunction: to edit this variable
void setFunction (int v)
{
tstVar = v;
}
getFunction: to return the variable
int getFunction ()
{
return tstVar
}
I have this code:
#include <stdio.h>
#include <pthread.h>
void* cuoco(void* arg)
{
fprintf(stderr,"Inizio codice cuoco\n");
fprintf(stderr,"Fine codice cuoco\n");
return NULL;
}
void* cameriere(void* arg)
{
fprintf(stderr,"Inizio codice cameriere\n");
fprintf(stderr,"Fine codice cameriere\n");
return NULL;
}
void* cliente(void* arg)
{
fprintf(stderr,"Inizio codice cliente\n");
fprintf(stderr,"Fine codice cliente\n");
return NULL;
}
int main(int argc, char* argv[])
{
void* (*routine)(void*);
routine=cuoco;
pthread_t thread_cuoco,thread_cameriere,thread_cliente;
pthread_create(&thread_cuoco,NULL,routine,NULL);
return 0;
}
And in the compiler options I insert -lpthread
But it says:
"Undefined reference to pthread_create"
I use ubuntu 10.10, so I already have pthread library installed, I can't figure the reason of this error.
Use -lpthread as the last compiler flag.
example:
gcc -o sample sample.c -lpthread
Without seeing the compiler command, I suspect -lpthread is not at end. Libraries need to be placed at end of the compiler command:
gcc main.c -lpthread
However, use -pthread instead of -lpthread, as -pthread may add other settings (like defining the macro _REENTRANT for example).
Use the following command:
gcc -pthread -o main main.c
In Eclipse, you should add string pthread.
Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC Linker -> Libraries -> Libraries (-l) -> Add -> pthread
After this, you can Build your project.
found the solution guys :D
just go to settings >> compiler >> linker tab >>add lib
go to drive and go to lib folder and find x86_64_linux_gnu and find pthread
enjoy :)
In libname.h:
int add_libname(int, int);
In libname.c:
#include "libname.h"
int add_libname(int a, int b)
{
return a+b;
}
I can build the shared library this way:
gcc -shared -fPIC libname.c -o libname.so
But I can't use it in another programe test.c:
#include <stdio.h>
#include "libname.h"
int main(int argc, char* argv[])
{
printf("%d\n", add_libname(1,5));
}
Reporting undefined reference to add_libname when I try to build it..
What's wrong here?
Because add_libname takes (int, int) you're giving it (1+5 = 6) or just (int)
I think you meant
add_libname(1, 5);
Also to compile it correctly you must use gcc like so
gcc -o myapp test.c -L. -lname
the lib part of libname is ignored as it is implicit
To create a shared library use these
gcc -fPIC -c libname.c
it gives warning: position independent code and libname.o file is generated.
and now type these command,
gcc -shared libname.so libname.o
libname.so ( the shared library is created with .so extension). To use the shared library
gcc -I/give the path of libname.h sourcefile.c /give the path of your .so file
example if your c file is file.c and the header file libname.h is in c:\folder1\project and your libname.so (shared library) is in c:\folder\project2
then
gcc -I/cygdrive/c/folder1/project file.c /cygdrive/c/folder/project/libname.so
this is the gcc command to be used while using the shared library.
Thank you.