Someone Random
Someone Random

Reputation: 21

Why does a function declared and included using header file show as undefined?

I have the following code in my files and all of these are in the same directory:

readMake.c:

#include <stdio.h>
#include "my_make.h"

int main(int argc, char* argv[]) {
    printSomething();
}

printer.c:

#include <stdio.h>

int printSomething(){
    printf("printed\n");
    return 1;
}

my_make.h:

int printSomething();

However, when I try to compile and run my code, I get the following error:

/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/prob1.dir/readMake.c.o: in function `main':
/cygdrive/c/Users/harsh/OneDrive/Documents/CSC352/assg9/prob1/readMake.c:5: undefined reference to `printSomething'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/prob1.dir/build.make:93: prob1.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/prob1.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/prob1.dir/rule] Error 2
make: *** [Makefile:124: prob1] Error 2

My IDE shows the proper signature of the printSignature function and where it is declared in the header file when I hover over the call in main, but the compilation doesn't seem to be working. Why is this happening and how can I resolve this?

Upvotes: 0

Views: 354

Answers (1)

Someone Random
Someone Random

Reputation: 21

I figured out a solution specific to CLion, unloading the CMakeLists.txt file and then creating a new one seems to resolve the issue.

I had to do 'Tools>CMake>Unload'

and then create a new CMakeLists.txt file which was automatically done by clicking an option appearing at the top of the window and selecting all of the files from a list of checkboxes.

Upvotes: 1

Related Questions