I have tried most of the suggestion regarding this issue to no avail.
I have created an Arduino project in CLion(version 2017.3.2) with Arduino plugin (version 1.2.3). I kept getting "undefined reference" during build.
My main Sample.ino file is:
#include <Arduino.h>
#include "Hello.h"
void setup() {
}
void loop() {
Hello::world();
}
and the Hello.h is simply:
#ifndef SAMPLE_HELLO_H
#define SAMPLE_HELLO_H
struct Hello {
static void world();
};
#endif //SAMPLE_HELLO_H
the Hello.cpp is
#include "Hello.h"
void Hello::world() {
}
and the CMakeLists.txt is:
cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(PROJECT_NAME Sample)
project(${PROJECT_NAME})
set(${CMAKE_PROJECT_NAME}_SKETCH src/Sample.ino)
#include_directories(include)
include_directories(src)
#### Uncomment below additional settings as needed.
set(${CMAKE_PROJECT_NAME}_BOARD mega)
set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyACM0)
set(mega.build.mcu atmega2560)
# set(mega.upload.protocol wiring)
set(mega.upload.speed 115200)
generate_arduino_firmware(${CMAKE_PROJECT_NAME})
All source files are under the folder src/,
Any suggestion to what have I missed?
I had the same problem and spent several hours to find a solution.
Arduino plugin uses arduino-cmake to build project. According to its documentation, to use generate_arduino_firmware command you should specify SKETCH or SRCS option.
If I define SKETCH option , I has no success to link additional files (hello.cpp).
So I try to use SRCS. Seems that if I define SKETCH and SRCS simultaneously, SRCS is be ignored. Another problem is when I specify src/sample.ino in SRCS option I have the same error.
So I do the following to compile the project successfully:
Renamed src/sample.ino into src/sample.cpp.
Commented out SKETCH and add SRCS option to list all source files:
set(${PROJECT_NAME}_SRCS src/sample.cpp src/hello.cpp)
Full CMakeList.txt is
cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(CMAKE_CXX_STANDARD 14)
set(PROJECT_NAME Sample)
project(${PROJECT_NAME})
set(${PROJECT_NAME}_BOARD uno)
set(${PROJECT_NAME}_SRCS src/sample.cpp src/hello.cpp)
set(${PROJECT_NAME}_PROGRAMMER arduinoasisp)
set(${PROJECT_NAME}_PORT COM17)
set(${PROJECT_NAME}_AFLAGS -v)
generate_arduino_firmware(${PROJECT_NAME})
The Arduino support plugin uses arduino-cmake which has a bug causing your SRCS and HDRS to be erased when compiling a sketch.
You can read my bug report and proposed fix here. Basically you want to modify cmake/Platform/Arduino.cmake
Change line 1808 to avoid overwriting your sources
set(ALL_SRCS ${SKETCH_SOURCES})
set(ALL_SRCS ${ALL_SRCS} ${SKETCH_SOURCES})
Change line 1821 to recompile when a source changes
DEPENDS ${MAIN_SKETCH} ${SKETCH_SOURCES}
DEPENDS ${MAIN_SKETCH} ${ALL_SRCS}
I had that problem too.
Adding this to the CMakeLists made it work:
include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
file(GLOB HDR_FILES ${PROJECT_SOURCE_DIR}/*.h)
set(PROJ_SRC ${SRC_FILES})
set(PROJ_HDR ${HDR_FILES})
generate_arduino_firmware(${CMAKE_PROJECT_NAME}
SRCS ${PROJ_SRC}
HDRS ${PROJ_HDR}
)
I'm not sure if that's the best solution but it worked in my case.
Related
The development for embedded system impose an other way to code.
In the goal to minimize size of my library named RF24Wave, I would adapt the structure of my main class.
The idea is to declare some functions only with the presence of certain #define during the inclusion of my library in main program.
The popular library, like MySensor use this way to minimize memory footprint.
So, I have two files for my library :
RF24Wave.h : header file which contain function declarations
#ifndef __RF24WAVE_H
#define __RF24WAVE_H
#include <arduino.h>
...
class RF24Wave
{
public:
/****** Common functions ******/
void begin();
#if !defined(WAVE_MASTER)
/****** Node functions ********/
void connect();
#else
/****** Master functions ******/
void printNetwork();
#endif
private:
bool synchronized = false;
}
RF24Wave.cpp: source file which contain function definitions
#include "RF24Wave.h"
void begin()
{
//Body of my function
}
#if !defined(WAVE_MASTER)
void connect()
{
//Body of my function
}
#else
void printNetwork()
{
//Body of my function
}
#endif
Secondly, I include this library in my main sketch named master.cpp, with #define WAVE_MASTER:
master.cpp: main sketch
#include <RF24.h>
#include <RF24Network.h>
#include <RF24Mesh.h>
#include <SPI.h>
#define WAVE_MASTER
#include <RF24Wave.h>
#include "master.h"
// Configure the chosen CE, CSN pins
RF24 radio(CE_PIN, CSN_PIN);
RF24Network network(radio);
RF24Mesh mesh(radio,network);
RF24Wave wave(network, mesh);
uint32_t displayTimer = 0;
void setup()
{
Serial.begin(115200);
wave.begin();
}
void loop()
{
wave.listen();
wave.printNetwork();
}
The goal is to include only master functions when #define WAVE_MASTER is defined in the main sketch.
However, during the compilation of my main sketch, I have a linking error
In function `main':
undefined reference to `RF24Wave::printNetwork()'
collect2: error: ld returned 1 exit status
*** [.pioenvs/uno/firmware.elf] Error 1
I compiled with PlaformIO Core 1.7.1 /Atom 1.13.0
Finally, the reason of this problem is the scope of #define.
The solution of this problem is added build flags to gcc-compiler !
If you use combo PlatformIO/Atom, you can add this line into this
config file platformio.ini :
build_flags = -D$NAME_DEFINE
In our case :
build_flags = -DWAVE_MASTER
After adding this line, the building seems work fine !
In addition, with this selective #define, my library has reduced footprint memory more than 10% !
Many thanks for your help.
If you add your library in the arduino IDE as it is described here it just consists in linking another project to your library functions. It's not a static library (see static and dynamic libraries). Then I think it's not necessary to worry about its size since the compiler will embed your library functions only if you use them.
Try opening any example (AnalogReadSerial), compile it. Then Sketch->Add a library->SPI. Compile it again, the size does not change. Try to call SPI.begin() in the setup function, the size increases. Add a call to SPI.setBitOrder(MSBFIRST);, the size increases again. Add another call to SPI.setBitOrder(MSBFIRST);, the size increases again, but not by the same amount since it contains only one setBitOrder definition and two calls to the setBitOrder function.
That's not exactly true for all libraries since some constructs could force the compiler to embed some code or allocate memory even if the variable is not used (see for instance volatile variables).
So regarding your size issue, you'd probably only need to use one #define MASTER, write the master code in setup and loop functions surrounded by #ifdef MASTER and the slave code surrounded by #else...#endif. The compiler will include the function definitions that both master or slave use.
So, with your answer, I found the solution !
The problem is the scope of #define. During the building, the first source code compiled is the main sketch ! So, it includes the correct #define WAVE_MASTER in RF24Wave.h of master.cpp. But, the compiler don't take this define into account of other source files. So, during the compilation of RF24Wave.cpp, #define WAVE_MASTER wasn't defined for this source file.
The solution consist to add an extra gcc-flag to define WAVE_MASTER for all source and header files.
With PlatformIO/Atom, it's possible in adding this line in config file platformio.ini
build_flags = -DWAVE_MASTER
Finally, your config file should look like this :
[env:uno]
platform = atmelavr
board = uno
framework = arduino
build_flags = -DWAVE_MASTER
With this solution, it is no longer necessary to add #define WAVE_MASTER in your header file, because this is the compiler which add it for you.
In addition, this optimization has been reduced more than 10% memory usage, because like this is a big class object, the compiler builded all functions. With selective define, the compiler will build only useful functions.
Anyone managed to compile library QtOpenCL using Qt5 (possibly under Window)?
edit: I managed to compile the code by porting it to QT5. I leave here in the question the dirty parts that I would like to replace in my method and describe what I did in the answer.
I was not able to provide the include path to my opencl installation so I
manually modified src/opencl/opencl.pro by adding the line
INCLUDEPATH += " *[absolute path to the openCL include folder]* "
QMAKE_LIBDIR_OPENCL = "*[absolute path to the opencl lib folder]*"
manually modified src/openclgl/openclgl.pro by adding the line
INCPATH += " *[absolute path to the openCL include folder]* "
QMAKE_LIBDIR_OPENCL = "*[absolute path to the opencl lib folder]*"
Anyone knows how to fix this in a cleaner way?
Here are the changes I had to introduce:
Modify the .pro files in order to add the OpenCL library. This is still an open problem for me. How to fix this in a cleaner way?
Link the projects (both opencl.pro and openclgl.pro) to the additional required Qt module QtConcurrent:
QT += concurrent
Fix the #include style by removing the packages, e.g. #include <qtconcurrentrun.h> instead of the old #include
<QtCore/qtconcurrentrun.h>
Fix qclvector.cpp by replacing qMalloc, qfree, qMemCopy with, respectively, std::malloc, std::free, std::memcpy
Moreover modify the initialization of ref in the constructor from ref = 1 to ref.store(1);
Removing all the macros QT_LICENSED_MODULE
This is enough to compile at least QtOpenCL and QtOpenCLGL using QT5
I'm struggling to get a subclass of QGLWidget compiled that uses glew. I'm running Qt 5.0.1 + MinGW 4.7 on Windows 7. Glew 1.7.0 has been compiled with said MinGW.
I have subclassed QGLWidget with UGGLWidget and included "GL/glew.h" before the QGLWidget includes. When compiling I get 200 Errors about:
'function' redeclared as different kind of symbol
conflicting declaration 'typedef'
So my Question:
How do I get a subclass of QGLWidget that uses glew compiled under Qt5?
Edit: I tried the same approach with another setup (Qt4.7 + MinGW4.4). It compiles and runs just fine. I have also tried using a more recent version of glew (Qt5.0.1 + MinGW4.7 + glew1.9.0) this doesn't compile either and gives the same compilation errors.
Here's a messy snippet of the compiler output:
> In file included from
> ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/QtGui/qopengl.h:55:0,
> from ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtOpenGL/qgl.h:47,
> from ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtOpenGL/QGLWidget:1,
> from src\UGGLWidget.h:4,
> from src\UGGLWidget.cpp:2: ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/GLES2/gl2.h:614:153:
> error: 'void __glewVertexAttribPointer(GLuint, GLint, GLenum,
> GLboolean, GLsizei, const GLvoid*)' redeclared as different kind of
> symbol In file included from src\UGGLWidget.cpp:1:0:
> ..\3rdPartyLibs\glew\include/GL/glew.h:13609:46: error: previous
> declaration of 'void (__attribute__((__stdcall__)) *
> __glewVertexAttribPointer)(GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid*)'
Last but not least the header and source file of the subclass. It's really just empty for now.
Source:
//UGGLWidget.cpp
#include "GL/glew.h"
#include "UGGLWidget.h"
UGGLWidget::UGGLWidget(QWidget *parent) : QGLWidget(parent) { ... }
Header:
//UGGLWidget.h
#ifndef UGGLWIDGET_H
#define UGGLWIDGET_H
#include <QGLWidget>
class UGGLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit UGGLWidget(QWidget *parent = 0);
};
#endif // UGGLWIDGET_H
Oh, and maybe relevant parts from the .pro file
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
LIBS += -L$$quote(../3rdPartyLibs/glew/lib/) -lglew32
LIBS += -lopengl32 -lglu32
INCLUDEPATH += $$quote(../3rdPartyLibs/glew/include)
The following fixed my problem, this goes everywhere, where I include glew:
#include "GL/glew.h"
#define QT_NO_OPENGL_ES_2
Another option: building Qt5.0.1 from source with -opengl desktop during the configure step.
For some reason QT_OPENGL_ES_2 is defined in my Qt5.0.1 installation. Looking through the inlcudes from QGLWidget.h I found the following in QtCore/qconfig.h
#if defined(QT_OPENGL_ES_2) && defined(QT_NO_OPENGL_ES_2)
# undef QT_OPENGL_ES_2
#elif !defined(QT_OPENGL_ES_2)
# define QT_OPENGL_ES_2
#endif
This leads to inclusion of GLES2/gl2.h which doesn't get along with glew. Knowing that the issue was related to GLES2 I came across a bug report and a QtProject post.
If I understand correctly, then the provided QtSDK installer for Qt5.0.1 has been built such that GLES is enabled.
Have a good day,
~Moritz
Note that QT 4.8RC release notes say:
Including QtOpenGL will not work in combination with GLEW, as QGLFunctions will undefine GLEW's defines.
I'm creating a new library to control the keypad and LCD together. Most of the code seems to compile, but when it reaches the line where I define the LiquidCristal variable, it says:
'LiquidCrystal' does not name a type when creating a custom library
This is the extract of content of my LCDKeypad.h
// Include types & constants of Wiring core API
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif
// Include LCD library
#include <LiquidCrystal.h>
The error is in this line:
private:
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); // <<-- Error
Alright, I was reading a lot and I found a very interesting article about this subject:
Including multiple libraries
It says that the compiler does not search for libraries that are not included in the sketch file. The way to hack this is to force the compiler to link them before loading your libraries, including, in my case LiquidCrystal.h in the sketch.
Let's say my library "LCDkeypad" requires LiquidCrystal.
My main program (sketch) needs to include LiquidCrystal in order to load it for my library "LCDKeypad".
Now, one interesting thing is using forward declaration, so in my LCDKeypad.h I will declare
"Class LiquidCrystal" but not include the library. I will do it in LiquidCrystal.cpp and in the sketch.
I hope this is clear.
There are two ways of doing it
If you are writing your own code Just create header file .h extension and relevant c code as name_c.While adding into main program, you need to specify header file name in double quotes.
code:
#ifndef LCD_H
#define LCD_H
//Declaration of variable /functions
#endif
If you are trying to download from link. Then you need paste the code into D:\arduino\arduino\libraries
Error problem:
overlapping of multiple declaration of variable.
overlapping of library functions
I am using Qt 4.5 in Windows XP. My pro file has the variable VERSION = 1.0. Now i need to read this variable and get its value (1.0) from the source code. So that I don't have to maintain another variable for version inside my source code. I just read from the pro file and update it. So that the value remains consistent all over my project. Is it possible? Any pointers regarding this are welcome..
Use somethings like this:
DEFINES += VERSION=\\\"$$VERSION\\\"
This will define a macro that you can use in C source code. Get rid of the backslashes and quotes if you want a number, not a string.
I'll elaborate on this a bit.
In the YourApp.pro:
VERSION = 0.0.0.1
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
In the main.cpp:
#include <QApplication>
QCoreApplication::setApplicationVersion(QString(APP_VERSION));
Wherever else in your sources, e.g. in the imaginary controller.cpp:
#include <QApplication>
QString yourAppVersion = QCoreApplication::applicationVersion();
somehow, when I tried qDebug() << QString(APP_VERSION); in a class.cpp not in main.cpp. has an error "C2065" APP_VERSION: undeclared identifier". but when I tried in main.cpp that worked.