Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why do I get "undefined reference to..." when trying to call a C-function inside a .cpp-file?

Why do I get "undefined reference to..." when trying to call a C-function inside a .cpp-file?

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiohelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. My project has 3 files, main.c, myDummyFile.h and myDummyFile.cpp. myDummyFile.h:

    #ifndef MYDUMMYFILE_H_
    #define MYDUMMYFILE_H_

    #ifdef __cplusplus
    extern "C" {
    #endif

    int myDummyFunction();

    #ifdef __cplusplus
    } //end extern "C"
    #endif

    #endif /* MYDUMMYFILE_H_ */

    myDummyFile.cpp:

    #include using namespace std;

    // Throw in some C++ syntax to make sure this file is compiled as a C++ file and not as C-file
    using cPlusPlusCallback_t = function ;

    volatile int preventOptimizationDummy = 0;

    int myDummyFunction() {
    return preventOptimizationDummy++;
    }

    main.c:

    #include
    #include
    #include "myDummyFile.h"

    int main(int argc, char* argv[]) {
    return myDummyFunction();
    }

    Does anybody know why I get the error message "undefined reference to `myDummyFunction'" when I try to compile this? If I remove the call to myDummyFunction() then it compiles fine so the compiler does seem to be able to compile .cpp-files.

    L 1 Reply Last reply
    0
    • A arnold_w

      I am writing code for an ARM processor and compiling with GCC inside Eclipse IDE. My project has 3 files, main.c, myDummyFile.h and myDummyFile.cpp. myDummyFile.h:

      #ifndef MYDUMMYFILE_H_
      #define MYDUMMYFILE_H_

      #ifdef __cplusplus
      extern "C" {
      #endif

      int myDummyFunction();

      #ifdef __cplusplus
      } //end extern "C"
      #endif

      #endif /* MYDUMMYFILE_H_ */

      myDummyFile.cpp:

      #include using namespace std;

      // Throw in some C++ syntax to make sure this file is compiled as a C++ file and not as C-file
      using cPlusPlusCallback_t = function ;

      volatile int preventOptimizationDummy = 0;

      int myDummyFunction() {
      return preventOptimizationDummy++;
      }

      main.c:

      #include
      #include
      #include "myDummyFile.h"

      int main(int argc, char* argv[]) {
      return myDummyFunction();
      }

      Does anybody know why I get the error message "undefined reference to `myDummyFunction'" when I try to compile this? If I remove the call to myDummyFunction() then it compiles fine so the compiler does seem to be able to compile .cpp-files.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to add the following line to myDummyFile.cpp:

      #include "myDummyFile.h"

      Otherwise the compiler will generate a decorated* name for myDummyFunction, because the source file is C++ not C. *In C++ external names have additional characters so the linker will not match what is called from your main file.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups