C: undefined reference to function

Seite 1 von 1 - Forum: Coding Stuff auf overclockers.at

URL: https://www.overclockers.at/coding-stuff/c-undefined-reference-to-function_255595/page_1 - zur Vollversion wechseln!


wergor schrieb am 20.04.2020 um 18:57

ich habe eine c library:

Code:
//tmp117.h

#ifndef TMP117_H_
#define TMP117_H_

//[...]

/*
 * resets and initializes a TMP117 sensor.
 * @return true on success (sensor responds), false otherwise
 */
bool TMP117Init(TMP117Settings *settings);

//[...]

#endif /* TMP117_H_ */
Code:
//tmp117.c

#include "tmp117.h"

bool TMP117Init(TMP117Settings *settings) {
  //[...]
}

//[...]
und benutze sie wie folgt:
Code:
#include "tmp117.h"

void setup() {
  // put your setup code here, to run once:

  TMP117Settings tmp117_settings[1];
  tmp117_settings[0].address = ADDRESS_0x48;
  
  for (int i = 0; i < 1; i++) {
    TMP117Init(&tmp117_settings[0]);
  }
}
das gefällt dem compiler nicht:
Zitat
C:\Users\wergor\Documents\Arduino\TMP117test/TMP117test.ino:11: undefined reference to `TMP117Init(TMP117Settings*)'
(arduino IDE)
Zitat
.pio\build\genericSTM32F103CB\src\main.cpp.o: In function `setup()':
main.cpp: (.text._Z5setupv+0xa): warning: undefined reference to `TMP117Init(TMP117Settings*)'
(platformIO)

woran liegt das? :confused:


COLOSSUS schrieb am 20.04.2020 um 19:06

Du musst deinem Compiler wohl auch noch mitteilen, dass er (bzw. sein Linker) die Library, deren Header du includest, beim Linken auch verwenden soll. Vermutlich: `g++ -ltmp117 <your_other_args_here>`. Evtl. brauchst du auch noch "-L<lib_dir>" im Argumentvektor, falls die Library nicht in den Default-Pfaden (kA unter Windows) installiert ist.


Vinci schrieb am 20.04.2020 um 19:18

Das liegt am Name Mangling von C++. Du braucht einen extern "C" Block rund ums include.


wergor schrieb am 20.04.2020 um 19:32

@colossus ich benutze die arduino ide und platformio, afaik kann ich da die parameter fürn compiler nicht so einfach ändern.
@vinci das wars! danke!




overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025