Unable to link to ncurses in shared object - c

For some reason I have having an issue compiling a shared object that uses ncurses. Even though I include and link with -lncurses, compiling the .so file fails. Please advise.
#include <string.h>
#include "../include/mod_curse.h" /* Includes ncurses.h and friends */
int OnConsoleCmd(McapiS *API, ArgS *CmdArgs) /* Just ignore these, they're included in mod_curse.h */
{
if(!strcmp(CmdArgs->Data, "help"))
{
API->BPrintf(STD, "\n-- mod_curse.so --\n");
return 0;
}
}
int OnLoad(McapiS *API, va_list Args)
{
initscr(); /* These are the problems */
}
/* Time to clean up and unload the module */
int OnDeload(McapiS *API, va_list Args)
{
endwin();
}
Here is the Makefile:
CC = clang
CFLAGS = -Wall -fPIC
# Object Files
OBJ = mod_curse.o
# Header Files
INCLUDE = include/mod_curse.h
# Main Module
mod_setup.so: $(OBJ) $(INCLUDE)
$(CC) -shared -Wl,-soname,mod_curse.so,--no-undefined -o ../../mod_curse.so -lncurses $(OBJ)
# Source Files
mod_curse.o: src/mod_curse.c $(INCLUDE)
$(CC) $(CFLAGS) -c src/mod_curse.c
clean:
rm $(OBJ)
Here are the errors:
3 warnings generated.
clang -shared -Wl,-soname,mod_curse.so,--no-undefined -o ../../mod_curse.so -lncurses mod_curse.o
mod_curse.o: In function `OnLoad':
src/mod_curse.c:(.text+0x81): undefined reference to `initscr'
mod_curse.o: In function `OnDeload':
src/mod_curse.c:(.text+0xb1): undefined reference to `endwin'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mod_setup.so] Error 1

I needed to change my make command to have -lncurses appear after $(OBJ).

Related

Did a Mac update ruin my ability to use gcc?

I am trying to compile two c files into one executable. In the directory I have only three files; Makefile, main.c and myfunction.c.
Makefile:
CC = gcc
CFLAGS = -Wall -g -O0
LIBS = -lm
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
MAIN = main
all: $(MAIN)
#echo Program has been compiled
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LIBS)
clean:
$(RM) *.o *~ $(MAIN)
main.c:
#include <stdio.h>
void myfunc();
int main (int argc, char *argv[]) {
myfunc();
return 0;
}
myfunction.c:
#include <stdio.h>
void myfunc() { printf("hello world"); }
output after make:
gcc -Wall -g -O0 -c -o main.o main.c
gcc -Wall -g -O0 -c -o myfunction.o myfunction.c
gcc -Wall -g -O0 -o main main.o myfunction.o -lm
Undefined symbols for architecture x86_64:
"_myfunc", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
I had something nearly identical working in the past. I have since clean installed MacOS and updated to Big Sur. Is this the issue or have I overlooked something?
I fixed the issue. I’m not sure what part fixed it, but installed Homebrew and used it to install gcc-10. I also deleted the project and started over.
myfunc would define like file header
myfunc.h
void myfunc()
Declare in another file
myfunc.c
void myfunc() { printf("hello world"); }
Follow the following tutorial
https://developer.gnome.org/anjuta-build-tutorial/stable/build-make.html.en

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?

Undefined reference resulting from g++ linking

I am new to g++ and Makefile. I am trying to link this BeBOP SMC library, which is in my lib directory. Under the lib directory are bebop_util and sparse_matrix_converter, both of which have already been built without errors. I see libbebop_util.a, libbebop_util.so under bebop_util and libsparse_matrix_converter.a, libsparse_matrix_converter.so under sparse_matrix_converter. Below is the source:
Makefile
CC=g++
CFLAGS=-c -Wall
test.out: test.o
$(CC) -o test.out -Ilib/sparse_matrix_converter/include -Llib/bebop_util \
-Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o
test.o: test.cpp
$(CC) $(CFLAGS) -Ilib/sparse_matrix_converter/include test.cpp
clean:
rm -f test.o test.out
test.cpp
#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>
int main(int argc, const char* argv[])
{
struct sparse_matrix_t* A = load_sparse_matrix (MATRIX_MARKET, "sample_input");
destroy_sparse_matrix(A);
return 0;
}
Output:
login3% make
g++ -c -Wall -Ilib/sparse_matrix_converter/include test.cpp
g++ -o test.out -Ilib/sparse_matrix_converter/include -Llib/bebop_util -Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o
test.o: In function `main':
test.cpp:(.text+0x1a): undefined reference to `load_sparse_matrix(sparse_matrix_file_format_t, char const*)'
test.cpp:(.text+0x27): undefined reference to `destroy_sparse_matrix(sparse_matrix_t*)'
collect2: ld returned 1 exit status
make: *** [test.out] Error 1
Please note that test.cpp depends on sparse_matrix_converter, which depends on bebop_util. Would you please let me know what mistakes I may have made? Thanks.
Tom
The BeBOP code looks to be C code but hasn't add the correct C++ guards. Surround your includes with extern "C" to fix that:
extern "C" {
#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>
}

Resources