How to use pinout with esp8266 from C file? - c++

I have a very basic example of the task I'm trying to solve. From the main loop in arduino.ino I call the sample_run() function in device.c using the definition in sample.h. From here I'm trying to understand how I can use the GPIO pins from device.c. Can this be done using the arduino ide? I've attached some sample code of what I'm trying to do.
arduino.ino
#include "sample.h"
#include "esp8266/sample_init.h"
void setup() {
}
void loop() {
sample_run();
}
sample.h
#ifndef SAMPLE_H
#define SAMPLE_H
#ifdef __cplusplus
extern "C" {
#endif
void sample_run(void);
#ifdef __cplusplus
}
#endif
#endif /* SAMPLE_H */
device.c
#include "sample.h"
void sample_run(void)
{
//I would like to turn on/off led here
}

In device.c: #include "Arduino.h"
Why are you using .c files and not .cpp?

Related

"multiple definition of" while variable is not defined anywhere else in the scope

I have these three source files:
test.h
#ifndef __TESTH
#define __TESTH
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
typedef struct {
uint8_t value;
} my_struct;
EXTERNC void initialise();
EXTERNC void load(my_struct**);
#endif
test.cpp:
#include <cstdint>
#include "test.h"
my_struct test;
void initialise() {
test.value = 200;
}
void load(my_struct** struct_ptr) {
*struct_ptr = &test;
}
main.cpp:
#include <cstdint>
#include <iostream>
#include "test.h"
my_struct *test;
int main() {
initialise();
load(&test);
while (true) {
std::cout << test->value << std::endl;
}
}
When I compile it, the linker gives me an error telling me that test has been defined multiple times (first defined in test.cpp).
Why? To me it seems like it doesn't leave the scope of test.cpp.
And when I remove the definition of test in main.cpp, it gives me an undefined error!
Thank you for taking the time out of your day to help me.
I think you would need to scope test.cpp's test variable to that file only, assuming your test pointer in main.cpp is different than test in test.cpp
namespace {
my_struct test;
}
See here

can i compile .cpp file in to a dll file where the cpp file itself calls the functions from another dll file

So getting in to detail of my question :
I have cpp file which calls functions from a dll file which is written in c.
Here is just an example code
first.cpp:
#include <stdio.h>
#include "Header.h"
#include <windows.h>
typedef double(*MYFUN1)(double);
int main1(int k )
{
MYFUN1 pfun1;
HMODULE hMod;
hMod = LoadLibrary(L"MyMathDll.dll");
pfun1 = (MYFUN1)GetProcAddress(hMod, "PowerOf3");
int ii, max = 20;
for (ii = 0; ii < max; ii++)
{
printf("%le", pfun1(10));
}
return ii;
}
header.h
#pragma once
int main1(int k);
My MyMathDll.dll is formed by following two files:
MyMathDll.c
#include "MyMathDll.h"
double PowerOf3(double UserNumber)
{
return UserNumber * UserNumber * UserNumber;
}
MyMathDll.h
#include <stdio.h>
#if defined (WIN32)
#if defined(FUNCTIONS_STATIC)
#define FUNCTIONS_API
#else
#if defined(FUNCTIONS_EXPORTS)
#define FUNCTIONS_API __declspec(dllexport)
#else
#define FUNCTIONS_API __declspec(dllimport)
#endif
#endif
#else
#define FUNCTIONS_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef MYMATHDLL_EXPORTS
#define MYMATHDLL_API __declspec(dllexport)
#else
#define MYMATHDLL_API __declspec(dllimport)
#endif
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
MYMATHDLL_API double PowerOf3(double UserNumber);
#ifdef __cplusplus
}
#endif
Now I am compiling first.cpp to a dll file, so it's basically i am trying make a dll file which already calls the functions from another c dll file.
I built the project a dll file is generated. Now I am making new project with application type .exe and trying load the dll file which is generated from first.cpp
example code of the second cpp which calls the functions from first.cpp
second.cpp
#include <stdio.h>
#include <windows.h>
typedef int (*MYFUN4)(int );
void main()
{
int k = 10;
MYFUN4 pfun4;
HMODULE hMod2;
hMod2 = LoadLibrary(L"first.dll");
pfun4 = (MYFUN4)GetProcAddress(hMod2, "main1");
printf("%d", pfun4(k));
getchar();
}
when i try to run this application, I get a pop window with following message:
Exception triggered at 0x00000000 in ConsoleApplication7.exe: 0xC0000005: Access violation when running at position 0x00000000.
If there is a handler for this exception, the program may still run safely.

Error when separating function into another source file in C

I have a simple project:
method.h:
#pragma once
#ifdef _METHOD_
#define _METHOD_
#include <stdio.h>
#include <conio.h>
int plus(int a, int b);
#endif // _METHOD_
method.cpp:
#include "method.h"
int plus(int a, int b)
{
return a+b;
}
Source.cpp:
#include <stdio.h>
#include <conio.h>
#include "method.h"
void main()
{
int a = plus(4, 5);
printf("%d",a);
printf("\n");
_getch();
}
but when I build project, an error occure:
I'm a newbie in C programming.
And so sorry about my grammar mistakes
remove
#ifdef METHOD
#define METHOD
as #pragma once does the same and if you want to use guards it should be
#ifndef ....
#ifdef _METHOD_ will ignore the header file as you are never defining "_METHOD_"
Update #1
As per MSDN on #pragma once;
Specifies that the file will be included (opened) only once by the
compiler when compiling a source code file.
Firstly, change "#ifdef METHOD" in your header file to "#ifndef METHOD"

'GLEWContext does not name a type' error on ubuntu

I am trying to port a glew_mx project from windows to ubuntu but I always get errors because of GLEWContext being undefined.
error: ‘GLEWContext’ does not name a type
I know that I dont really need GLEWContext on linux but nevertheless I have to define
GLEWContext* glewGetContext();
in order to compile my project. So I created a global GLEWContext and simply return it in glewGetContext.
My window.h code looks like this:
#pragma once
#define GLEW_MX
#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#define GLM_SWIZZLE
#include "glm/glm.hpp"
#include "glm/ext.hpp"
#ifdef _WIN32
#define CONTEXT_PREFIX window
#else
#define CONTEXT_PREFIX
#endif
namespace window
{
class Window
{
public:
Window() {}
~Window() {}
//...
#ifdef _WIN32
static void makeContextCurrent(Window* window_handle);
#endif
static Window* createWindow(int win_width, int win_height, const std::string& title, GLFWmonitor* monitor, Window* share);
GLFWwindow* window;
#ifdef _WIN32
GLEWContext* glew_context;
#endif
//...
private:
//...
};
GLEWContext* glewGetContext();
#ifdef _WIN32
//...
#else
GLEWContext* glew_context;
#endif
}
And the code in window.cpp looks like this:
#ifdef _WIN32
GLEWContext* window::glewGetContext()
{
//...
}
#else
GLEWContext* window::glewGetContext()
{
return glew_context;
}
#endif
The error occurs while compiling the last two lines in window.h
Many thanks for your help
It seems the compiler compiles the your Windowclass and gets to the GLEWContext* glew_context line. But GLEWContext may not be defined, so forward declaration might be helpful.
Since you are porting from windows to ubuntu you have to be sure that the #pragma is supported by your compiler. You may change your include guard to
#ifndef WINDOW_H
#define WINDOW_H
// Your code here
#endif

ANSI C code for both Objective-C and C++ code?

In my project there are 3 possible types of files: pure C/Objective-C, pure C++ or Objective-C++ code.
How to divide functions in .h file with #define directives into parts to make this file available for all these files? I don't want to rename all the .m files to .mm because of problems with refactoring.
I know that I can write .h file in C which uses C++ .cpp file using the following code:
#ifndef Chadstone_CCCWrapper_h
#define Chadstone_CCCWrapper_h
#ifdef __cplusplus
#include <string.h>
extern "C"
{
#endif
void minMaxCoordinates(char *c, float *minX, float *minY, float *maxX, float *maxY);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
but what if I want to add functions with using of NSString or list<...>.
You can find it in standard pch-file genarated by Xcode:
#ifdef __OBJC__
#endif
Also you need use CF_EXPORT macro when declaring function to prevent linkage errors.
Example:
#ifndef SOME_H_FILE
#define SOME_H_FILE
#ifdef __OBJC__
#interface SomeObjClass: NSObject
#end
CF_EXPORT void SomeFunctionWithNSString(NSString* str);
#endif
#ifdef __cplusplus
class SomeCPlusPlustClass
{
};
CF_EXPORT void someFunctionWithList(const list<int>& intList);
#ifdef __OBJ__
CF_EXPORT void someComplicatedFunction(NSString* str, const list<int>& intList);
#endif
#endif
CF_EXPORT void someFunction();
typedef struct _SomeStruct
{
} SomeStruct;
#endif