0

I am a beginner programmer, I am doing homework and I ran into a problem with undefined reference.

I have 1 header and two sorce files:

main.c, utils.c, utils.h

I am supposed to write a function to check prime numbers from 16 to 235 in utils.c and call the function in main.c.

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "utils.h"


int main()
{
    is_prime();
    return 0;
}

utils.c

#include <stdio.h>
#include <stdlib.h>


void is_prime()
{
    int i, n[220], v = 220, broj, k = 2,z = 16;
    for (i = 0; i < v; i++)
    {
        n[i] = z++;
    }
    for (i = 0; i < v; i++)
    {
        broj = n[i];
        while ((broj % k) != 0 && k < broj) k++;
        if (k == broj) printf("%d ", i, n[i]);
        k = 2;
    }
}
utils.h

    #ifndef UTILS_H_INCLUDED
    #define UTILS_H_INCLUDED

    void is_prime();

    #endif // UTILS_H_INCLUDED
Clifford
  • 88,407
  • 13
  • 85
  • 165
Tomica
  • 1
  • 2
    Undefined reference to what? Post the full error message. And how you compile it. – Eugene Sh. Mar 10 '21 at 17:52
  • 2
    Full error traceback? Compilation command? – SuperStormer Mar 10 '21 at 17:52
  • "undefined reference" is a linker error, not a C language error. It is your build command that is not correct - you need to compile both sources then link them. Show your build command - in fact show the entire build log. – Clifford Mar 10 '21 at 17:54
  • ||=== Build: Debug in code_skeleton_codeblocks_lab3 (compiler: GNU GCC Compiler) ===| obj\Debug\main.o||In function `main':| C:\Users\www\Downloads\code_skeleton_codeblocks_lab3\main.c|54|undefined reference to `is_prime'| ||error: ld returned 1 exit status| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| – Tomica Mar 10 '21 at 17:56
  • 1
    @Tomica : In future respond to issues with your question by editing the question not adding comments. – Clifford Mar 10 '21 at 17:58
  • @Tomica you should Edit your question and add the build command / error traceback to it so that readers don't have to read all the comments section ;) (Welcome to StackOverflow) – limserhane Mar 10 '21 at 17:58
  • @Tomica : The log is incomplete - you probably need to do a re-build all to get a more complete log. It does not show the command line driving the compiler/linker. Is that the raw text log or some filtered output by the IDE? Have you added utils.c to your Code::Blocks project? It looks like you are only linking main.o and utils.o is not compiled or linked. Just including the header file does not cause utils.c to be compiled or linked. – Clifford Mar 10 '21 at 18:04

0 Answers0