makefile error when using string.h - c

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

Related

1 I am doing the cs50 course and decided to run the code from there on my MacBook [duplicate]

This question already has an answer here:
CS50 get_string error linker command failed with exit code 1
(1 answer)
Closed 13 days ago.
I have the following code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x = get_int("whats x? ");
int y = get_int("whats y? ");
if (x < y)
{
printf("x is less than y\n");
}
}
It keeps giving me this error
moutarrjallow#Moutarrs-MBP cclang % make ccompare
cc ccompare.c -o ccompare
Undefined symbols for architecture x86_64:
"_get_int", referenced from:
_main in ccompare-91f0e5.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: *** [ccompare] Error 1
You need to link with the cs50 library by adding the -lcs50 to above. In your Makefile you can probably do that by setting LDLIBS := -lcs50.

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

how to solve this problem compiler error that linker command failed in VSCODE

I wrote some C code on VSCODE like that.
The code is divided into three files: the header, the function, and main in same project folder.
But when I started compile, files are can't compile and error. like terminal text.
Maybe I think this error is linking error..
How to solve this problem..?
[source code]
mysqrt.c
#include "mysqrt.h"
#include <stdio.h>
#include <math.h>
double mysqrt(double a, double b){
double result = sqrt(pow(a,2)+pow(b,2));
return result;
}
mysqrt.h
#include <stdio.h>
double mysqrt(double a, double b);
mysqrtTest.c
#include <stdio.h>
#include <math.h>
#include "mysqrt.h"
void main(void){
double sum = mysqrt(3,4);
printf("%.2f\n",sum);
}
[terminal text]
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: warning:
return type of 'main' is not 'int' [-Wmain-return-type]
void main(void){
^
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: note: change
return type to 'int'
void main(void){
^~~~
int
1 warning generated.
Undefined symbols for architecture x86_64:
"_mysqrt", referenced from:
_main in mysqrtTest-45c3c1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[OS]: macOS Mojave 10.14.6
to resolve your linking issue you need to compile all your c file, as dependencies are not automatically resolved ( header files could be name separately from code file so .h files and .c files are independant ).
# assuming that gcc is your compiler
gcc -Wall -Wextra -Werror -pedantic -o mysqrtTest mysqrtTest.c mysqrt.c -lm
Although I would recommend you to learn about separate compilation, and using a build system, like make
Example of Makefile
mysqrtTest: mysqrtTest.o mysqrt.o
${CC} -o $# $^ -lm
then use make to build your binary

How do I compile with external header file in sublime text in C?

I use one header named header.h in main.c.
The function test is announced in header.h and defined in test.c.
However, it says words below even though I use build system as C.
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"test(int)", referenced from:
_main in main-a4d82e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.6s with exit code 1]
[cmd: ['bash', '-c', 'g++ -Wall -std=c++11 -O2 \'/Users/hanlock/Documents/CODE/TEST/TEST/main.c\' -o \'/Users/hanlock/Documents/CODE/TEST/TEST/main\' && osascript -e \'tell application "Terminal" to activate do script " cd \\"/Users/hanlock/Documents/CODE/TEST/TEST\\" &&start_ms=$(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\")&&clear&&\\"/Users/hanlock/Documents/CODE/TEST/TEST/main\\" &&elapsed_ms=$(($(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\") - start_ms))&& read -p \\"Press Enter to exit($elapsed_ms ms).\\"&&exit"\'']]
[dir: /Users/hanlock/Documents/CODE/TEST/TEST]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
I've read similar questions about this in C++. However, it seems not working in my case.
So, here comes the problem. How can I use external header file with sublime in language C?
Here is the code:
main.c
#include <stdio.h>
#include "./Header.h"
int main(int argc, const char * argv[]) {
test(1);
return 0;
}
header.h
#ifndef Header_h
#define Header_h
int test(int);
#endif
test.c
#include <stdio.h>
int test(int i){
printf("%d\n",i);
return 0;
}
In test.c:
#include "header.h"
In your shell:
gcc -I [dir] test.c
This will include any external header named header.h located in dir.
I hope this answers your question.

Link tidylib library to C application

I am trying to use the tidylib library within my C application. When compiling, I get the following errors:
$ make
rm -f sbo-export
cc sbo-export.c safarilib.c -L/usr/local/lib -lcurl -L/usr/local/Cellar/libtidy/lib -I/usr/local/Cellar/libtidy/include -o sbo-export
Undefined symbols for architecture x86_64:
"_tidyCreate", referenced from:
_safari_init_session in safarilib-c7ab6a.o
"_tidyParseString", referenced from:
_safari_init_session in safarilib-c7ab6a.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: *** [default] Error 1
safarilib.c:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <tidy/tidy.h>
#include <tidy/buffio.h>
#include <curl/curl.h>
#include "safarilib.h"
void usage( char *username, char *password )
{
TidyDoc tdoc = tidyCreate();
char *input = "<html><body><h1>Hello World!</h1></body></html>";
tidyParseString( tdoc, input );
}
Any suggestions?
Further Infos:
For installing libtidy, I did the following:
Downloaded libtidy from http://tidy.sourceforge.net and then
followed the instructions form the file tidy/build/readme.txt
My library is installed here:
/usr/local/Cellar/libtidy
/usr/local/Cellar/libtidy/bin
/usr/local/Cellar/libtidy/bin/tab2space
/usr/local/Cellar/libtidy/bin/tidy
/usr/local/Cellar/libtidy/include
/usr/local/Cellar/libtidy/include/buffio.h
/usr/local/Cellar/libtidy/include/platform.h
/usr/local/Cellar/libtidy/include/tidy.h
/usr/local/Cellar/libtidy/include/tidyenum.h
/usr/local/Cellar/libtidy/lib
/usr/local/Cellar/libtidy/lib/libtidy-0.99.0.dylib
/usr/local/Cellar/libtidy/lib/libtidy.a
/usr/local/Cellar/libtidy/lib/libtidy.dylib
/usr/local/Cellar/libtidy/lib/libtidy.la
From compiler option it's look you have not specified tidylib.
cc sbo-export.c safarilib.c -L/usr/local/lib -lcurl -L/usr/local/Cellar/libtidy/lib -I/usr/local/Cellar/libtidy/include -o sbo-export
Here you need to add -ltidy and probably path by -L .
Some additional info about linking.

Resources