Undefined symbols for architecture x86_64 termcap.h in C - c

I have this code:
#include <stdio.h>
#include <termcap.h>
#include <stdlib.h>
int main() {
char *termtype = getenv("TERM");
int li = tgetnum("li");
printf("%d", li);
return 0;
}
I can't compile it because I get
Undefined symbols for architecture x86_64:
"_tgetnum", referenced from:
_main in main-11560e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Why is it happening?
MacOS Catalina

Related

Can't use "get_int" in libcs50 for C

So I'm currently new to C right now, and I just downloaded the cs50 library. I tried to make an input by using get_int(), but it's not working. Here's my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int i = get_int("Input: ");
printf("Output: %i\n", i);
}
This is the error that I got
cc calculator.c -o calculator
Undefined symbols for architecture x86_64:
"_get_int", referenced from:
_main in calculator-225c65.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: *** [calculator] Error 1

Undefined symbols for architecture x86_64: "_swap", referenced from: _main in swap-315897.o ld: symbol(s) not found for architecture x86_64

I'm trying to reproduce the code of the book Effective C: An Introduction to Professional C Programming by R. Seacord
#include <stdio.h>
void swap(int, int); // defined in Listing 2-2
int main(void) {
int a = 21;
int b = 17;
swap(a, b);
printf("main: a = %d, b = %d\n", a, b);
return 0;
}
When I compile the code on my MacOS 10.15.7 with gcc swap.c, I get the following error:
Undefined symbols for architecture x86_64:
"_swap", referenced from:
_main in swap-315897.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What shall I do to correct it?

compiling a program that includes libmodbus in C

I am a newbie in C... I wrote a very simple modbus1.c that includes libmodbus (whose source I downloaded, unzipped, untarred, ./configure'd, make'd and make install'd successfully).
When I try to make modbus1.c I get this:
cc -Wall -g modbus1.c -o modbus1
Undefined symbols for architecture x86_64:
"_modbus_close", referenced from:
_main in modbus1-6cd135.o
"_modbus_connect", referenced from:
_main in modbus1-6cd135.o
"_modbus_free", referenced from:
_main in modbus1-6cd135.o
"_modbus_new_tcp_pi", referenced from:
_main in modbus1-6cd135.o
"_modbus_read_bits", referenced from:
_main in modbus1-6cd135.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: *** [modbus1] Error 1
I am running OSX snow leopard and have successfully used make to compile small programs before (tutorial level programs...) Here is the modbus1.c I am trying to compile:
#include <stdio.h>
#include <stdlib.h>
#include <modbus.h>
int main(int argc, char *argv[]){
modbus_t *plc_client;
plc_client = modbus_new_tcp_pi("192.168.1.230","502");
if (plc_client == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\n");
return -1;
}
if (modbus_connect(plc_client) == -1) {
fprintf(stderr, "Connection failed: \n");
modbus_free(plc_client);
return -1;
}
else if(modbus_connect(plc_client) == 0) {
printf("MODBUS CONNECTION SUCCESSFUL\n");
}
uint8_t* catcher = malloc(sizeof(uint8_t));
if(modbus_read_bits(plc_client, 2000, 1, catcher)>0){
printf("READ SUCCESSFUL");
}
else{
printf("READ FAILED");
}
free(catcher);
modbus_close(plc_client);
modbus_free(plc_client);
return 0;
}
Any help will be greatly appreciated! Thanks!
-Niko
Try this
cc -Wall -g modbus1.c -o modbus1 -L/path/to/libmodbus -lmodbus
You should replace that /path/to/libmodbus with the actual path of directory that includes the libmodbus.dylib in your system.

makefile error when using string.h

I realize this has probably been answered but I just cannot find it. When using "make" to compile the following file:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// get line of text
string s = GetString();
// print string, one character per line
for (int i = 0; i < strlen(s); i++)
{
char c = s[i];
printf("%c\n", c);
}
return 0;
}
I get the following message:
$ make example
cc example.c -o example
Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in example-iPNXBe.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: *** [example] Error 1
Judging from the cs50 page you probably need something like -lcs50 at the end of the cc command.
Or you can just use the cs50.o object file and link with it.
cc example.c cs50.o -o example

GSL and gsl_linalg_cholesky_invert

I'm using the gsl library in a c code. Everything goes fine but when i use the command gsl_linalg_cholesky_invert the terminal shows the following message:
Undefined symbols for architecture x86_64:"_gsl_linalg_cholesky_invert", referenced from:
_main in cc4eefuQ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
A code example is the following.
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_linalg.h>
#include <math.h>
void main()
{
gsl_matrix * A = gsl_matrix_calloc (2,2);
gsl_matrix_set(A,0,0,1);
gsl_matrix_set(A,0,1,0.5);
gsl_matrix_set(A,1,0,0.5);
gsl_matrix_set(A,1,1,1);
gsl_linalg_cholesky_decomp(A);
gsl_linalg_cholesky_invert(A);
}
If i comment out the line gsl_linalg_cholesky_invert(A);, everything goes fine.
I compile the code with the following command:
gcc-mp-4.7 wrapnorm.c -o wrapnorm -lgsl -l gslcblas -lm
with a macbook pro with mountain lion and gsl version 1.9.
Thanks

Resources