0

My source file starts with:

#include "foo.h"

I am able to use all of my global variables, types, and structs from my header file. In my header file i started with a couple of includes:

#include<file.h>

Then move on to global variables:

#define GLOBAL

Then structs:

#typedef struct boo;

Then types. Then i go to function declarations. I.E:

size_t foo(int*r, size_t nitems);

What am I doing wrong that i am getting linker errors>

4
  • I cannot help but wonder what the #define GLOBAL is for? Commented Jul 15, 2011 at 15:25
  • The #define global is just a global varibale definition, I have written: #define Global 5 But i am getting a LNK2001 Commented Jul 15, 2011 at 15:29
  • Please distill your program to a minimal, complete sample the demonstrates the problem and post that here, along with whatever compiler and linker errors it produces. For information about how to do that, and why it is important, see sscce.org. Commented Jul 15, 2011 at 17:00
  • 1
    You are including the header files, so the objects compile. But you need to include libraries for the objects to link to for the system (<>) header files with a -l compiler switch. Commented Jul 16, 2011 at 4:26

3 Answers 3

1

The problem's presumably in how you're linking.

Sign up to request clarification or add additional context in comments.

Comments

1

The header file provides the function declaration to your source code. As you've found that's enough to get your code to compile but not to get it to link.

To get it to link you have to provide the linker with a file containing the actual compiled function - the function declaration is effectively a promise to the compiler that you will do this.

Exactly how you do it depends on what tools you're using and what form the compiled function is in.

Comments

0

#define for simple substitution identifiers works exactly like a copy/paste.

After you do

#define GLOBAL 5

the following code

int GLOBAL;
GLOBAL = 13;
putchar(GLOBAL);

gets transformed to

int 5;
5 = 13;
putchar(5);

which obviously does not compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.