-1

I have a .cpp file which I declare a function

#ifndef MyFile_hpp
#define MyFile_hpp

#ifdef __cplusplus
extern "C" {
#endif

void runCode();

#ifdef __cplusplus
}
#endif

#endif 

This works fine. I can call this function within my Objc. In my implementation I have an extern void that it will not let me call. It's giving the undefined symbols. I need this to build as is, the extern will be declared in a different file upon compiling. Having the extern, should this compiler just trust me and let me build?

#include "MyFile.h"

extern int runMe();

#ifdef __cplusplus
extern "C" {
#endif

void runCode() {
   runMe();
}

#ifdef __cplusplus
}
 #endif
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
mKane
  • 932
  • 13
  • 30
  • 1
    Is the first shown code a .cpp file (as stated in the question body), a header named "MyFile.hpp" (as implied by the reinclusion guard) or a header named "MyFile.h" (the name used in the include line) ? – Yunnosch Apr 27 '18 at 15:48
  • The first file named MyFile.h and second file is named MyFile.cpp – mKane Apr 27 '18 at 15:53
  • Can you show the definition of the function `runMe();`? I don't see it in the quoted code. I feel with the compiler/linker. – Yunnosch Apr 27 '18 at 15:55
  • Is the shown code compiled by a C++ compiler or a C compiler? Is the extern compiled by C++ compiler or a C compiler? – Yunnosch Apr 27 '18 at 15:57
  • What happens if you move the declaration of `runMe()` into the extern "C"? – Yunnosch Apr 27 '18 at 15:58
  • The runMe is not declared anywhere yet. It will be declared inside of a .c file. It is compiled by Xcode – mKane Apr 27 '18 at 16:09
  • It still fails if declared inside of the extern "C" – mKane Apr 27 '18 at 16:11
  • It is declared in the second non-empty line of the second code quote, by giving its prototype, with or without `extern` does not matter. If it "will be" defined after the linker attempts to link your code, then it is not yet at linking. – Yunnosch Apr 27 '18 at 16:11
  • So when compiling the compiler needs to be able to find that function somewhere? – mKane Apr 27 '18 at 16:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169962/discussion-between-mkane-and-yunnosch). – mKane Apr 27 '18 at 16:15
  • https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Yunnosch Apr 27 '18 at 16:38

1 Answers1

0

I think what you are doing is probably correct.

The compiler doesn't just trust you. A C++ function is declared to the linker using additional characters, or "decorations" that define the full detail of the cclass it is a member of and all its arguments. A C function has none of that, just the raw name, so it is easy for the linker to tell the difference.

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
  • Calling the extern in .cpp gives this Undefined symbols for architecture arm64: – mKane Apr 27 '18 at 16:04
  • What symbol does it say is undefined? – Gem Taylor Apr 27 '18 at 16:09
  • Undefined symbols for architecture arm64: "runMe()", referenced from: _runCode in MyFile.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – mKane Apr 27 '18 at 16:10
  • So you have to write your c++ function runMe in another .cpp file? – Gem Taylor Apr 27 '18 at 16:11
  • I will write that function runMe in a C file that hasn't been created yet – mKane Apr 27 '18 at 16:13
  • So surprise, you are being told it is undefined. Please define it :-) You wrote the extern for runMe outside extern "C" {}, which tells the c++ compiler it is a c++ function, so you will need to write a c++ function, or move the extern.. – Gem Taylor Apr 27 '18 at 17:38