gcc -lcrypt flag error in UNIX C - Undefined Reference to crypt() - c

I use crypt() function and the compile flag named -lcrypt the problem is that the compiler says undefined reference to crypt(). Can anyone tell me what I'm doing wrong?
Makefile
CC = gcc
CFLAGS=-Wall -lm -lcrypt
OBJS = get_passwords_hashed.o
PROG = get_passwords_hashed.exe
#adicionar or mudar o OBJS se tiver outras files para o programa
#GENERIC
all: ${PROG}
clean:
rm ${OBJS} *~ ${PROG}
${PROG}: ${OBJS}
${CC} ${OBJS} -o $#
.c.o:
${CC} $< -c -o $#
# $# - turns .c into .o
###################################
#dependencias
so_final.o: get_passwords_hashed.c get_passwords_hashed.h
main.c
#include <stdio.h>
#include <string.h>
#include <crypt.h>
int testar_pass(char ant[],char (*pointer_hashes)[max_chars_string]){ // ponteiro para array de chars - char ** ant
char * password ;
char * encrypted;
password = malloc(strlen(ant)*sizeof(char)); //password calculada recebida anteriror
encrypted = malloc(strlen(ant)*sizeof(char));//hash
strcpy(password,ant);
encrypted = crypt(password,"10");
if(strcmp(*pointer_hashes,encrypted) == 0){
return 1;
}
else return 0;// devolve erro
}

Pass -lm -lcrypt at the end of your compilation line.
LIBS=-lm -lcrypt
${CC} ${OBJS} -o $# ${LIBS}
EDIT:
The explanation of why it makes a difference (as requested in a comment) from gcc manual:
-llibrary
[...]
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the
order they are specified.
Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file ‘foo.o’ but before ‘bar.o’. If ‘bar.o’ refers to functions in ‘z’, those functions
may not be loaded.

Related

Makefile's output explanation

I wrote the following makefile:
MAIN = main
# Macro nome compilatore
CC = gcc
# Macro flag del compilatore
CFLAGS = -Wall
# Macro file oggetto
OBJS = $(MAIN).o
# Macro libreria matematica
LIBS = -lm
# Target
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(LIBS) -o $(MAIN)
When i run it for the first time, the output is:
gcc -Wall -c -o main.o main.c
gcc -Wall main.o -lm -o main
Can you explain me the first line please? What does it mean?
Thank you for your time.

undefined references when using fftw3.3.6 with threads and float enabled

I installed fftw3.3.6 on my Ubuntu 16.04 to test the performance of using this library with thread and float enabled.
Step 1 :
Installed the library with thread and float and SIMD instructions enabled:`
sudo ./configure --enable-float --enable-generic-simd128 --enable-generic-simd256 --enable-threads
make
make install
Step 2 :
I wrote this code (basing on the manual and tutorial) to compute an fft of 1024 points using 4 threads (complex to complex):
#include <stdio.h>
#include <stdlib.h>
#include <fftw3.h>
#include "input.h"
#define NUMBER_OF_ELEMENTS 1024
#define NUM_THREADS 4
void Load_inputs(fftwf_complex* data)
{
int i;
for(i=0;i<NUMBER_OF_ELEMENTS;i++)
{
data[i][0] = input_data[2 * i];
data[i][1] = input_data[2 * i + 1];
}
}
int main()
{
fftwf_complex array[NUMBER_OF_ELEMENTS];
fftwf_plan p;
int i;
fftwf_init_threads();
fftwf_plan_with_nthreads(NUM_THREADS);
p = fftwf_plan_dft(1,NUMBER_OF_ELEMENTS,array,array,FFTW_FORWARD,FFTW_EXHAUSTIVE);
Load_inputs(array); //function to load input data from input.h file to array[]
fftwf_execute(p);
FILE* res = NULL;
res = fopen("result.txt", "w");
for ( i = 0; i <1024; i++ )
{
fprintf(res,"RE = %f \t IM = %f\n",array[i][0], array[i][1] );
}
fclose(res);
fftwf_destroy_plan(p);
fftwf_cleanup_threads();
}
And then, I compiled this program with this makefile.
CC=gcc
CFLAGS=-g3 -c -Wall -O0 -mavx -mfma -ffast-math
SOURCES=$ test.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=test
all: $(TASKMAP) $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# -L -lfftw3f_threads -lfftw3f
.c.o:
$(CC) $(CFLAGS) $< -lm -o $#
clean:
rm -fr $(OBJECTS) $(EXECUTABLE)
Compilation errors :
After compilation I've got these errors:
gcc test.o -o test -L -lfftw3f_threads -lfftw3f
//usr/local/lib/libfftw3f.a(mapflags.o): In function `fftwf_mapflags':
mapflags.c:(.text+0x346): undefined reference to `__log_finite'
Makefile:13: recipe for target 'test' failed
//usr/local/lib/libfftw3f.a(trig.o): In function `cexpl_sincos':
trig.c:(.text+0x2c1): undefined reference to `sincos'
//usr/local/lib/libfftw3f.a(trig.o): In function `fftwf_mktriggen':
trig.c:(.text+0x50b): undefined reference to `sincos'
trig.c:(.text+0x653): undefined reference to `sincos'
test.o: In function `main':
/home/anouar/workspace/Thread_example//test.c:27: undefined reference to `fftwf_init_threads'
/home/anouar/workspace/Thread_example//test.c:28: undefined reference to `fftwf_plan_with_nthreads'
/home/anouar/workspace/Thread_example//test.c:40: undefined reference to `fftwf_cleanup_threads'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
Is there something missing, or something wrong that I have did during installation and compilation?
Read the fine manual. From the sincos() man page:
Link with -lm.
Using -lm in the compile phase of your program is useless:
.c.o:
$(CC) $(CFLAGS) $< -lm -o $#
-lm needs to be in the link stage:
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# -L -lfftw3f_threads -lfftw3f -lm
In my case, the problems with
undefined reference to `__log_finite'
could be solved by compiling without the -ffast-math option.

"undefined reference" error compiling a static library in C

I have a file main.c, a header rippledp.h and a library rippledp.a. The problem is: when I execute the "make" command, I get this output:
g++ -O2 -DNDEBUG -static -o rippledp main.o rippledp.a -lm -pthread
main.o: In function `main':
main.c:(.text+0x24): undefined reference to `rippledp_read'
main.c:(.text+0x39): undefined reference to `rippledp'
main.c:(.text+0x43): undefined reference to `rippledp_write'
collect2: ld returned 1 exit status
make: ** [rippledp] Erro 1
Here is the Makefile:
#--------------------------------------------------#
# Ripple-DP (ISPD2015 contest version) #
# Copyright (c) 2015 #
# Department of Computer Science and Engineering #
# The Chinese Univeristy of Hong Kong #
# #
# Contact: #
# Wing-Kai Chow <wkchow#cse.cuhk.edu.hk> #
# Evangeline F.Y. Young <fyyoung#cse.cuhk.edu.hk> #
#--------------------------------------------------#
OPT= -O2 -DNDEBUG
#OPT= -O0 -ggdb
TYPE= -static
#WFLAG= -Wall -Winline
CC= g++ $(OPT) $(TYPE) $(WFLAG) $(DEBUG)
LIBS= -lm -pthread
SRCS = ${OBJS:%.o=%.c}
BFILE = rippledp
all: $(BFILE)
#$(BFILE): main.o rippledp.a libdef.a liblef.a
# $(CC) -o $(BFILE) main.o rippledp.a libdef.a liblef.a $(LIBS)
$(BFILE): main.o rippledp.a
$(CC) -o $(BFILE) main.o rippledp.a $(LIBS)
%.o : %.c %.h
$(CC) -c $*.c
clean:
rm -f *.o $(BFILE) core
Here is main.c:
#include "rippledp.h"
int main(int argc, char **argv){
/* read benchmark files: tech.lef, cells.lef, floorplan.def */
/* read global placement solution: placed.def */
rippledp_read((char*) "tech.lef", (char*) "cells.lef", (char*) "floorplan.def", (char*) "placed.def");
/* detailed placement with target utility and maximum displacement constraint */
rippledp(0.8, 200000);
/* write the detailed placement solution to output file */
rippledp_write((char*)"dplaced.def");
return 0;
}
And here is rippledp.h:
/*--------------------------------------------------*/
/* Ripple-DP (ISPD2014 contest version) */
/* Copyright (c) 2014 */
/* Department of Computer Science and Engineering */
/* The Chinese Univeristy of Hong Kong */
/* */
/* Contact: */
/* Wing-Kai Chow <wkchow#cse.cuhk.edu.hk> */
/* Evangeline F.Y. Young <fyyoung#cse.cuhk.edu.hk> */
/*--------------------------------------------------*/
#ifndef _RIPPLEDP_H_
#define _RIPPLEDP_H_
/*read benchmarks and placed global placement solution*/
void rippledp_read(char *tech_file, char *cell_file, char *floorplan_file, char *placed_file);
/*Perform displacement-constrained legalization and detailed placement*/
/* target_util = target utility */
/* max_disp = maximum displacement constraint */
void rippledp(double target_util, double max_disp);
/*write placement result in DEF format*/
void rippledp_write(char *output_file);
#endif
I also tried to compile and link manually. I first compiled using:
gcc -c main.c
Then, I tried all these alternatives for linking (I renamed rippledp.a to librippledp.a):
gcc -o out -L. -lrippledp main.o
gcc -o out -L. main.o -lrippledp
gcc -o out main.o -L. -lrippledp
gcc main.o -o out -L. -lrippledp
gcc -o out -lrippledp -L. main.o
gcc -lrippledp -o out -L. main.o
and the output was the same.
I dont have access to the library content.
Your library is compiled with C++ and thus contains C++ mangled names. But you compiled main.c as C, so it looked for unmangled names and thus couldn't find them. Rename main.c to main.cpp and compile it with g++ to fix this issue.

Strange error in makefile for C program which connect to MQ onm Linux

I have created a new makefile
CC = /usr/bin/gcc
MQM_HOME = /opt/mqm
CFLAGS = -g -Wall \
-I$(MQM_HOME)/inc \
-I/usr/include
#IBM_LIBPATH=-L/usr/vacpp/lib
IBM_LIBS= -liconv
MQ_LIBPATH=-L$(MQM_HOME)/lib64
MQLIB = $(MQ_LIBPATH) -lmqm -lmqmcs -lmqmzse
LIBS = $(IBM_LIBS) $(MQLIB) -lpthreads
#LDFLAGS = -q64 $(IBM_LIBPATH) $(MQ_LIBPATH)
SOURCE=/home/avalanche/oleg
default: ctm_mq_con_ex
ctm_mq_con_ex: ctm_mq_con_ex.o
# Compilation rules
EXE = $(SOURCE)/ctm_mq_con_ex
MAIN = $(SOURCE)/ctm_mq_con_ex.c
OBJS = $(SOURCE)/ctm_mq_con_ex.o
.c.o:
$(CC) $(CFLAGS) -c $(MAIN)
$(CC) $(CFLAGS) -o $(EXE) $(OBJS)
clean:
\rm -f $(OBJS)
\rm -f $(EXE)
Now it shows errors
make -f ./ctm_mq_con_ex.mk
/usr/bin/gcc -g -Wall -I/opt/mqm/inc -I/usr/include -c /home/avalanche/oleg/ctm_mq
/usr/bin/gcc -g -Wall -I/opt/mqm/inc -I/usr/include -o /home/avalanche/oleg/ctm_mq
/home/avalanche/oleg/ctm_mq_con_ex.o: In function `main':
/home/avalanche/oleg/ctm_mq_con_ex.c:67: undefined reference to `MQCONNX'
/home/avalanche/oleg/ctm_mq_con_ex.c:86: undefined reference to `MQOPEN'
/home/avalanche/oleg/ctm_mq_con_ex.c:114: undefined reference to `MQINQ'
/home/avalanche/oleg/ctm_mq_con_ex.c:142: undefined reference to `MQCLOSE'
/home/avalanche/oleg/ctm_mq_con_ex.c:163: undefined reference to `MQDISC'
collect2: ld returned 1 exit status
But in my C program I put header files
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
/* includes for WebSphere MQ */
#include <cmqc.h> /* For regular MQI definitions */
#include <cmqxc.h> /* For MQCD definition */
Did I miss something?

SHA1 library link not found using makefile

I'm having trouble linking the sha library with my makefile while compiling.
Here is my makefile:
CFLAGS= -g -Wall -Werror -std=c99 -pedantic
LDFLAGS=-lssl -lcrypto
CC = gcc
LD = gcc
OBJS = dhtnode.o
PROG = dhtnode
.c.o:
gcc $< -o $# $(CFLAGS)
all: $(PROG)
$(PROG): $(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $(PROG)
dhtnode.o: dhtnode.c dhtpackettypes.h
$(CC) $(CFLAGS) $(LDFLAGS) dhtnode.c
clean:
/bin/rm -f *.o dhtnode
My function using the lcrypto library is here:
#include <openssl/sha.h>
#include <stdlib.h>
#include <stdin.h>
//there are other includes but not concerning this part of the code
char sha() {
char *ibuf = malloc(sizeof(char));
ibuf ="172.0.0.1:11112";
char *obuf = malloc(SHA_DIGEST_LENGTH);
SHA1((unsigned char*)ibuf, strlen(ibuf), (unsigned char*)obuf);
int i;
for (i = 0; i < 20; i++) {
printf("%x" , (unsigned char)obuf[i]);
}
printf("\n");
return *ibuf;
}
Here is the error I get when building with Eclipse:
C/p2p/dhtnode.c:107: undefined reference to `SHA1'
Can anybody tell my what is wrong with my makefile or possible eclipse settings?
Thx in advance!
When compiling the object file, you don't need the LDFLAGS. You'll also need the -c compiler flag to produce an object file instead of linking a binary:
dhtnode.o: dhtnode.c dhtpackettypes.h
$(CC) $(CFLAGS) -c dhtnode.c
After making this change, the program compiles and links successfully for me.

Resources