• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Using WiFiServer in multiple files in PlatformIO

Q&A Forum › Category: ESP32 › Using WiFiServer in multiple files in PlatformIO
0 Vote Up Vote Down
Henk van Beek asked 4 years ago

I want to use WiFiServer and in the main file and in a second cpp file.
When I declare it in the main and and in the main.h file with the extern directive I get error:
redefinition of ‘WiFiServer server’

6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
I think this discussion might help:

  • https://community.platformio.org/t/tutorial-for-creating-multi-cpp-file-arduino-project/5830/4

Regards,
Sara

0 Vote Up Vote Down
Henk van Beek answered 4 years ago

This is how to do split a program into muliple files. Ok, I know.
But my question is specific about WiFiServer.
How and where to declare/define this so it is reachable within main.cpp and a module.cpp.

When I declare it in main.cpp and in main.h( with the extern directive) I get an error

“redefinition of ‘WiFiServer server’”. The h-file contains a wrapper of course.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
I’m not sure how to solve that issue.
Were you able to solve it yet?
I think it is better to ask for help on the PlatformIO forum for ESP32: https://community.platformio.org/
I’m sorry that I can’t help much.
Regards,
Sara

0 Vote Up Vote Down
Steve Mercer answered 4 years ago

You may like to try compiler directives in both files:

#ifndef server
WiFiServer server
#endif

A useful tutorial on compiler directives can be found at https://www.deviceplus.com/arduino/arduino-preprocessor-directives-tutorial/

0 Vote Up Vote Down
Henk van Beek answered 4 years ago

My error:
In main.cpp I declared “WiFiServer(80)”
and in main.h “extern WiFiServer(80)”.
The last I had to change into “extern WiFiServer server;”
Now it is correct.
Thanks.
 

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
I’ll close this issue.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.