简体   繁体   中英

Compiling C programs with static files

I am trying to compile ac program with a static library and its not working .

This is the error :

undefined reference to `calculatearea' collect2.exe: error: ld returned 1 exit status .

The static files were made with the gcc / g++ compilers .

This is the main code :

#include <stdio.h>
#include <stdint.h>

int calculatearea(int a , int b);
int main()
{


int c = calculatearea(2,4);

printf("%d",c);
getchar();
return 0;
}

edit : : screenshot of compiler error

From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.

To solve this:

1) Write the definition for function calculatearea in the same file.

2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.

3) As mentioned in the picture if the area.o have the definition of function calculatearea , then compile as below, this will generate a.out in linux:

gcc filename.c area.o 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM