-1

I have a C file which contains multiple functions. The example structure of mymain.c is as follows:

#include "myheader.h"
int main(int argc, char *argv[]) {
    int var1;
    struct var2;

    func1(var1, &var2);
    if(condition)
        func2(var1);
    else
        calc(var1, &var2);

    return 0;
}

static void func1(int var1, struct *var2){

    // do something
}

static void func2(int var1){

    // do something

}

char * func3(char * c){

    // do something
}

static void calc(int var1, struct *var2){
    int myvar = 5;
    char mystring[100]="....";
    func1(var1, var2);
    func3(mystring);
    func2(myvar);
}

I want to move the calc() and func3(char *c) to a separate C file. Following the answers here I moved the two functions to a calc.c file, created a calc.h file with the calc() signature and did a #include "calc.h"in mymain.c file. Also included "calc.h" in calc.c.

The structure of calc.c is now:

#include "myheader.h"
#include "calc.h"

char * func3(char * c){

    // do something
}

void calc(int var1, struct *var2){
    int myvar = 5;
    char mystring[100]="....";
    func1(var1, var2);
    func3(mystring);
    func2(myvar);
}

When I try and build it with NetBeans it shows the following error:

In function `main':
mymain.c:(.text+0x238): undefined reference to `calc'
collect2: error: ld returned 1 exit status
Makefile:426: recipe for target 'mymain' failed
make[2]: *** [mymain] Error 1
Makefile:504: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
Makefile:393: recipe for target 'all' failed
make: *** [all] Error 2

What is going wrong here?

hw-135
  • 162
  • 1
  • 15
  • 1
    care that you compile both of your .c files, and if you just started using .h files, please read about guard clauses ;) – Angevil Nov 12 '19 at 14:42
  • 2
    Ok, after the edit: Do not include a .c file, never. You should just compile them together like ```gcc file1.c file2.c``` – Angevil Nov 12 '19 at 14:44
  • "_When I try and build it_" - Show the setup of that build. – underscore_d Nov 12 '19 at 14:45
  • @rb-93 please post your header file as well. – Roman Dmitrienko Nov 12 '19 at 14:48
  • 2
    I don't think this question can be salvaged unless all code is deleted, then all actual code from the real project is pasted here. – Lundin Nov 12 '19 at 15:03
  • In addition to suggestions about making sure you compile and everything, what about the `func1` and `func2` functions? They are still declared `static` in "mymain.c", but are called from the `calc` function in "calc.c". That won't work. – Ian Abbott Nov 12 '19 at 15:04
  • There are no such types as `struct` or `struct *`. – Ian Abbott Nov 12 '19 at 15:06
  • It's not possible to post the actual code. Hence the use of an example code. It seems to be creating confusion rather than clarification. Apologies. – hw-135 Nov 12 '19 at 15:08
  • 1
    @rb-93 it's ok that you don't post the actual code, but you should post a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). That is, write something that reproduces the problem and post it. But be sure it *does reproduce the problem before posting it*. – Federico klez Culloca Nov 12 '19 at 15:11

1 Answers1

2

The reason is that the static functions cannot be seen from other translation units. Remove the static declaration, and it will link.

The name of your question also mentions shared variables. If you want to share a variable between translation units, make sure to declare it as extern in your header file, and to define it exactly once in one of your source code files.

Roman Dmitrienko
  • 3,375
  • 3
  • 37
  • 48
  • 1
    @rb-93 then, the most likely reason is that you have forgotten to include calc.c in your project file, and it is not being built. – Roman Dmitrienko Nov 12 '19 at 14:50
  • Where should I include `calc.c`? I included `calc.h` and `myheader.h` after @Angevil's suggestion. – hw-135 Nov 12 '19 at 14:52
  • @rb-93 Sorry, I didn't mean `#include`-ing it :) Check out your project settings in Netbeans. There must be the "Files" tab in the UI that contains, well, the files. You'll probably see "mymain.c" but not "calc.c" there. – Roman Dmitrienko Nov 12 '19 at 14:55
  • the whole `static` as typo ... interesting. Do you have a key with the whole word `static` – 0___________ Nov 12 '19 at 14:56
  • @P__J__, this is just the example structure, not the actual code of course. I copied the `calc.c` from the `main.c` and hence the typo. – hw-135 Nov 12 '19 at 14:57
  • @rb-93 but did you check your project structure? – Roman Dmitrienko Nov 12 '19 at 15:11
  • @RomanDmitrienko, the project contains `calc.c` and `calc.h`. – hw-135 Nov 12 '19 at 15:14
  • 1
    @rb-93 at this point it really looks like a project issue. Try making a new project from scratch and including all those files there. Otherwise, it is hard to answer your question without seeing the actual source code and the project file, unfortunately. – Roman Dmitrienko Nov 12 '19 at 15:19