Does the IDE include c files?

I'm compiling a bunch of code and there are some .c files in the bunch. Everything compiles, but the linker doesn't seem to be bringing in the code from the .c files. Is there something I need to do to make .c code link in?

I see where others use .c files so it must be doable.

thanks!

-jim lee

extern "C" / C++ name mangling.

Wait, how do I do that for an entire file?

-jim lee

You don't need to do it for the entire file; you need to do it in the .h file that defines the APIs for that .c file.

Just put

#ifdef __cplusplus
extern "C" {
#endif

At the beginning of the .h file describing the API, and

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

at the end of the file.

Also, make sure none of your .c files overlap the same names as your .ino or .cpp files.
Having a "myprogram.ino" and "myprogram.c" is a bad idea.

That does't seem to be the issue.

I see other libraries use these from .cpp file without issue. The only thing I see them doing is..

extern "C" {
  #include "bsp/include/nm_bsp.h"
  #include "bsp/include/nm_bsp_arduino.h"
  #include "driver/source/nmasic.h"
  #include "driver/include/m2m_periph.h"
  #include "driver/include/m2m_wifi.h"
  #include "driver/include/m2m_ssl.h"
}

in the .cpp & .h files

I think doing the same thing, but I must be missing something.

Error log.

LC_WiFi_Example:13: warning: deprecated conversion from string constant to 'char*' 
/var/folders/v_/nvnwrxts2c52kkh49p5666y80000gn/T/arduino_build_518336/libraries/LC_WiFi/WiFiObj.cpp.o: In function `WiFiObj::WiFiObj(int, int, int)':
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:47: undefined reference to `gi8Winc1501CsPin'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:47: undefined reference to `gi8Winc1501IntnPin'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:47: undefined reference to `gi8Winc1501ResetPin'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:47: undefined reference to `gi8Winc1501ChipEnPin'
/var/folders/v_/nvnwrxts2c52kkh49p5666y80000gn/T/arduino_build_518336/libraries/LC_WiFi/WiFiObj.cpp.o: In function `WiFiObj::init()':
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:103: undefined reference to `nm_bsp_init'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:106: undefined reference to `m2m_wifi_init'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:109: undefined reference to `m2m_ssl_set_active_ciphersuites'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:123: undefined reference to `nmdrv_firm_ver'
/var/folders/v_/nvnwrxts2c52kkh49p5666y80000gn/T/arduino_build_518336/libraries/LC_WiFi/WiFiObj.cpp.o: In function `WiFiObj::handleProvisionInfo(void*)':
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:163: undefined reference to `m2m_wifi_connect'
/var/folders/v_/nvnwrxts2c52kkh49p5666y80000gn/T/arduino_build_518336/libraries/LC_WiFi/WiFiObj.cpp.o: In function `WiFiObj::begin(char*, char*)':
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:70: undefined reference to `m2m_wifi_connect'
/Users/jimlee/Documents/Arduino/libraries/LC_WiFi/WiFiObj.cpp:76: undefined reference to `m2m_wifi_handle_events'
collect2: error: ld returned 1 exit status
Error compiling for board Teensy 3.2 / 3.1.

Compiles fine, won't link.

P.S. I did try adding the #ifdef __cplusplus stuff and saw no change.

-jim lee

All files placed in a sketch directory...

test.h

#ifndef __TEST_H__
#define __TEST_H__

#ifdef __cplusplus
extern "C" {
#endif

int dummy( void );

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

#endif

test.c

#include "test.h"

int dummy( void )
{
  static int rv;
  return ++rv;
}

whatever.ino

#include "test.h"

void setup() 
{
  Serial.begin( 250000 );
}

void loop() 
{ 
  Serial.println( dummy() );
}

Success...

Sketch uses 1724 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.

What is the name of the file that contains gi8Winc1501CsPin?

What is the name of the file that contains nm_bsp_init?

nm_bsp_init() is in nm_bsp_arduino.c

I tried this..

In my .cpp file..

extern "C" {
  #include "bsp/include/nm_bsp.h"
  #include "bsp/include/nm_bsp_arduino.h"
  #include "driver/source/nmasic.h"
  #include "driver/include/m2m_periph.h"
  #include "driver/include/m2m_wifi.h"
  #include "driver/include/m2m_ssl.h"
}

extern "C" {
	int8_t gi8Winc1501CsPin;
	int8_t gi8Winc1501ResetPin;
	int8_t gi8Winc1501IntnPin;
	int8_t gi8Winc1501ChipEnPin;
	int8_t nm_get_firmware_info(tstrM2mRev* M2mRev);
	int8_t m2m_wifi_connect(char *pcSsid, uint8 u8SsidLen, uint8 u8SecType, void *pvAuthInfo, uint16 u16Ch);
	sint8	nm_bsp_init(void);
}

The extern C for the .h files was how the file I was looking at seemed to accomplish this. Its been in my file since the beginning. I added the second block and that seems to have fixed the trouble with the variables but not the functions.

The files are from WiFi101. I'm rewriting the Wifi class from scratch so I can understand it better. Then I can add a normal Berkley sockets interface to it. Hopefully one I can make work.

-jim lee

jimLee:
I tried this..

Works for me...

test.h

#ifndef __TEST_H__
#define __TEST_H__

/*
#ifdef __cplusplus
extern "C" {
#endif
*/

int dummy( void );

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

#endif

test.c

#include "test.h"

int dummy( void )
{
  static int rv;
  return ++rv;
}

.ino

extern "C" {
#include "test.h"
}

void setup() 
{
  Serial.begin( 250000 );
}

void loop() 
{ 
  Serial.println( dummy() );
}

jimLee:
nm_bsp_init() is in nm_bsp_arduino.c

Where is that file in your directory structure?

Put an obvious syntax error in that file then try to build. Do you get an error?

Good idea. I'll try that.

All the c files are in sub folders of the one I'm working on.

-jim lee

jimLee:
All the c files are in sub folders of the one I'm working on.

I think that does not work. Try to create a src directory in the sketch directory. Under that src directory you can create library directories in which you can place h and c files.

Wait, I mean..

What I'm working on is a library. There is a .cpp & .h I'm writing and a bunch of source folders along side of it. The sketch is by itself in a sketch folder.

Now I notice the library I started from has all this code & folders in a src folder. Could that possibly change anything? WTH, Ill try it.

Oh! I also found the #ifdef c++ { in
-jim lee

Zip up your library and post it so we see what you're doing. I'm not going to play 20 questions to figure out your directory structure and header arrangement, and I'm surprised that this thread has gone through a dozen replies with none of your code posted.

Calm down jiggly! I Got it!

This was the ticket..

My lib folder was "LC_WiFi" Now, typically I just dump .ccp & .h files in library folders and everything just works. This time I had to follow someone else's crazy format. So I dumped all their source files into my folder and added my stuff along side of them. This seems t work fine as long as everything is .cpp or .h files. It seems that it just ignores the .c files.

Now sterretje made the suggestion that use a src folder inside my library folder. I noticed that this is how they did it. With everything in the source folder. I tried i with my stuff outside the folder and that was a hot mess. So copying their format I put all the source on the src folder. Then it couldn't even find my include in the .ino file.

What to do? Look for differences. I notice that my LC_WiFi folder was NOT showing up on the library list so there is something odd going on. I notice the library.properties file, open it and edit it to match what I'm writing. Presto! Everything works!

So what I gather is.. Setting up a folder and dumping .cpp & .h files in it to create a library works fine. But once it gets subfolders & .c files, you need the scr folder with the matching library.properties file for it to work.

Thanks for the help everyone!

-jim lee