0

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

dvhh
  • 4,593
  • 25
  • 32
김동균
  • 23
  • 5
  • Don't use pictures of code — include the code as text in the question. Likewise, don't use pictures of error messages (though you seem to include the compilation error message in the question too). – Jonathan Leffler Dec 09 '19 at 01:07
  • Hello, welcome to stackoverflow, it would be better for you to put your code directly into your question instead of putting image. – dvhh Dec 09 '19 at 01:08
  • 1
    You need to compile `mysqrtTest.c`and `mysqrt.c` in the same command line. – dvhh Dec 09 '19 at 01:09
  • Images of code and error messages are useless to us. See [this Meta post](https://meta.stackoverflow.com/a/285557/62576) for a list of the many reasons why. Code and errors are text, and can be copied/pasted directly into your post. You'll find your experiences here will be much better if you spend some time taking the [tour] and reading the [help] pages to learn how the site works before posting. – Ken White Dec 09 '19 at 01:12
  • The 'return type of main' message is a warning — only on Windows systems is `void main(void)` valid. See [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) for the full details. – Jonathan Leffler Dec 09 '19 at 01:12
  • sorry, this community I first time using.. and I don't speak English well.. – 김동균 Dec 09 '19 at 01:20
  • how to compile mysqrtTest.c and mysqrt.c in the smae command line?? – 김동균 Dec 09 '19 at 01:24
  • `gcc -Wall -Wextra -Werror -pedantic -o mysqrtTest mysqrtTest.c mysqrt.c` – dvhh Dec 09 '19 at 01:38
  • Although I would recommend you learn to use Make – dvhh Dec 09 '19 at 01:38
  • 1
    Need to link in the math library. – chux - Reinstate Monica Dec 09 '19 at 01:44

1 Answers1

1

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

dvhh
  • 4,593
  • 25
  • 32