0

I need to have a couple arrays in a file and I want to be able to access it from another C file. I have defined them in a header file, arrays.h and then I initialize them in a arrays.c file. The problem is, when trying to access the array's indices from another file, I get a unresolved external ... LNK2001 error. I think it's because, the 'arrays.c' file is never compiled, so my program knows the array exists, but cannot access it because that file 'arrays.c' is never called so the array is not initialized.

Am I right? Is there any way to fix this problem keeping the source like it is?

1
  • 1
    Then it seems the arrays.c file is not part of the project properly. Otherwise it should have been compiled and linked together with the other files. Also, make sure your spelling is correct. Commented Apr 20, 2013 at 23:51

1 Answer 1

3

If the arrays are defined in arrays.c but the program using them is in program.c, you will need to compile both arrays.c and program.c and link the object files together.

On a Unix-like system, that might be:

cc -o program program.c arrays.c

Or:

cc -c program.c
cc -c arrays.c
cc -o program program.o arrays.o

The error message looks suspiciously like a message from MSVC; in that case, you need to follow the appropriate rules to achieve the same effect as shown by the Unix commands.

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

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.