1

Possible Duplicate:
Why does const imply internal linkage in C++, when it doesn’t in C?
What is external linkage and internal linkage in C++

I have a two C files that I'm trying to compile to an executable. One file contains only a single declaration as follows (simplifed).

const char *foo[2] = {"thing1", "thing2"};

The second c file does this

extern const char *foo[2];
main()
{
 //Code that does stuff with foo
}

When compiling I get a linker error that foo is an unresolved external symbol. I'm assuming the compiler is optimizing out foo. Any ideas here?

Community
  • 1
  • 1
Brandon Yates
  • 2,022
  • 3
  • 22
  • 33
  • 1
    How do you compile? You need to link with the object file from the `.c` file containing the definition. – Daniel Fischer Dec 14 '12 at 22:27
  • 3
    What command are you using to compile? It sounds like you're only compiling and linking the second file, not the first. – Chris Dodd Dec 14 '12 at 22:27
  • First I compile the two files separetly (it's our make system that does it this way). then it does this 'link.exe file1.obj file2.obj extlib.lib /out:out.exe' – Brandon Yates Dec 14 '12 at 22:29
  • 1
    @YePhIcK: That is related, but *what?* and *why?* are different questions. – Ben Voigt Dec 14 '12 at 22:35
  • Don't tag this question `c` if you're compiling as `c++`. – Ben Voigt Dec 14 '12 at 22:35
  • However it is a duplicate of http://stackoverflow.com/questions/9032475/const-and-global and http://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c – Ben Voigt Dec 14 '12 at 22:38
  • The question was tagged `C` (I'm guessing here) because the code is `C` in author's eyes (missing the fact that the tools used to build the code are indeed `C++` and `C` is mostly forward compatible with `C++`) – YePhIcK Dec 14 '12 at 22:42
  • 1
    Both linked answers are irrelevant. This question is not about internal linkage for `const` objects in C++. The object `foo` in the above declaration is not `const`. The linking behavior of these declarations does not differ between C and C++. – AnT stands with Russia Dec 14 '12 at 22:45
  • Any suggestion then Andrey? – Brandon Yates Dec 14 '12 at 22:52
  • sorry i see your response below now – Brandon Yates Dec 14 '12 at 22:52

1 Answers1

4

There's nothing wrong with your declarations. The code should compile and link as is, assuming you add explicit int as the return type of your main. The only explanation for the linker error I can come up with is that you are forgetting to supply all required object files to the linker.

The answers that attempt to explain this issue through the fact that in C++ const objects have internal linkage are misleading and irrelevant. The above object foo is not a const object.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thanks for the response. Do you know if the order object files are passed to the linker matter? FOr example is there a difference between "link.exe file1.obj file2.obj lib1.lib" and "link.exe file2.obj file1.obj lib1.lib" – Brandon Yates Dec 14 '12 at 23:11
  • 1
    @Brandon Yates: It matters for GNU linker (or maybe even any POSIX linker?) when it is processing *libraries*. I'm not sure about MS linkers. But when the input is just obj files (no libraries) the the order should does not matter with any linker. If the library is specified/processed last (as in your case) you should be OK with any linker. – AnT stands with Russia Dec 14 '12 at 23:15
  • The order shouldn't matter. If the order matters you have bigger issues in your code (mostly related to initialization dependencies and duplicate symbols' resolutions) – YePhIcK Dec 14 '12 at 23:15