Redefinition of function error

I am using two stacks to implement a queue class. My header file looks like: #ifndef _MyQueue_h #define _MyQueue_h using namespace std; template class MyQueue { public: M...

In C and C++, #include behaves like a copy and paste.
Everytime you see

#include "file" 

it should be treated as if you literally retyped the whole file in that one spot.
So if you compile MyQueue.cpp, the preprocessor will prepend the contents of MyQueue.h,
which itself tacks on a duplicate of MyQueue.cpp evidenced by

#include "MyQueue.cpp" 

and then follows the native content of MyQueue.cpp.

So the result of

#include "MyQueue.cpp"

inside MyQueue.h, is the same as if you had written one big file with the contents
of MyQueue.h, MyQueue.cpp and MyQueue.cpp again. (with the include of stack in there as well of course)
That is why the compiler complained about functions getting redefined.

The Duplicate inserted from the

#include "MyQueue.cpp" 

might also contain the line

#include "MyQueue.h"

but I think the include guards (ifndef,endif) protected against a recursive expansion since that did
not seem to be an issue.

I would like to point out that putting all the implementation code and declaration code in the same file for templates is not the only solution, as others suggest.

You just have to remember that templates are generated at compile time and include them wherever they are needed. Like Aaron has pointed out, you can even force generate a template for a specific type or function so it’s accessible to all units.

In this way, the definition of a function can be embedded in an arbitrary module and the rest of the modules won’t complain that a function isn’t defined.

I like to declare small templates and template interfaces in header files
and put large implementations in special files that are just glorified headers. You could put some special extension like .tpp .cppt or whatever to remind yourself that it is code you have to include somewhere (which is what I do).

It is a suitable alternative to storing large implementations in header files that must be pasted around just to refer to the type (or function signature). And it works absolutely fine, for years now.

So for example, when I am ready to compile my big program, I might have a file called structures.cpp that I designate to implement lots of small structures I use, as well as instantiate all the templates for my project.

all the other .cpp files in the project need to include «mylib/template_structs.h» in order to create instances of templates and call functions with them. whereas structures.cpp only needs to include «mylib/template_structs.cppt» which in turn may include template_structs.h
or else structures.cpp would have to include that as well first.

If structures.cpp calls all the functions that any other .cpp files would call for that template then we are done, if not, then you’d need the extra step of something like

template class mynamespace::queue<int> ;

to generate all the other definitions the rest of the project’s modules would need.

Topic: Error: redefinition of function  (Read 28139 times)

I have a problem that I think is related to my IDE. I am using Codeblocks (13.12).

Here’s my «set up»:
main.c (includes ‘int main()’)
header.h (your typical header, includes prototypes)
test.c (a random file, includes custom-made functions.)

Here’s the issue: All the functions works as intended, but when I compile my test.c I get an error (for each function) saying: «error: redefinition of ***»
This issue doesn’t affect anything, but it’s annoying. I’m wondering if it’s possible to get rid of it somehow? Maybe I’m doing something wrong when I’m creating my prototypes?

Here’s an example of what my functions and prototypes look like:

void func_showMenu();  //This is the prototype, in header.h

void func_showMenu(){
  //This is the function, in test.c
}

Is there some setting in Code::Blocks that can fix this issue?


Logged


Please post the complete contents of both your header and source files.  If each function pair is the same then one complete pair will suffice.  Make sure to include the boiler-plate code like the: define that is supposed to be in a header.

« Last Edit: December 21, 2015, 07:28:07 pm by headkase »


Logged


« Last Edit: December 21, 2015, 09:30:02 pm by stahta01 »


Logged

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.

When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org


I created a few test files to show the error:

Here’s main.c:

#include "header.h"

int main(){
    test();
    testTwo();

    return 0;
}

header.h:

#ifndef header.h
#define header.h

#include <stdio.h>
#include <stdlib.h>

#include "test.c"

void test();
void testTwo();

#endif

test.c:

#include "header.h"

void test(){
    printf("Test works! n");
}

void testTwo(){
    printf("TestTwo also works! n");
}

This is all there is, nothing more, nothing less.

When I run the program (main.c) it outputs:
«Test works!»
«TestTwo also works!»

So the program runs just fine.
But. Whenever I build (ctrl+F9) test.c I get the following errors:

C:…test.c|3|error: redefinition of ‘test’|
C:…test.c|3|note: previous definition of ‘test’ was here|
C:…test.c|7|error: redefinition of ‘testTwo’|
C:…test.c|7|note: previous definition of ‘testTwo’ was here|
||=== Build failed: 2 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

If I add more functions in test.c I will get one error like this for each function added.
Like I said: the program still runs and all, but it is very annoying.
Is there some way to fix this?


Logged



Logged


There are very rare cases where you need to include *.c or *.cpp files.
Remove this include and it should work, if there are no other beginner errors.


Logged


So I removed

from the header file, and got these errors:

C:…main.o:main.c|| undefined reference to `test’|
C:…main.o:main.c|| undefined reference to `testTwo’|
||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

The program doesn’t run now, and it can’t seem to find my functions in test.c anymore.


Logged


hi Phrosen,

you completely miss the point.

It is possible to declare a function as often as you like throughout the code, but what you are doing wrong here, is that you define it more than once.

The include directive copies the text literally into the file.

If you examine closely in your code this leads to multiple definition of test and testTwo.

First you include it in main.c via the #include header.h in which there is an #include test.c which holds the definition.

Then in test.c you #include header.c again, which has test.c included with the same definition all over again. Its a bit recursive here, which makes it confusing to understand, but the compiler breaks of after the first recursion, when he finds the first redefinition of test and testTwo.

Solution is simple: don’t include test.c in header.h

In order to find the actual implementation of the functions, you must make the implementation known to the compiler on the command line when invoking gcc.

In the case of compiling with c::b give the paths to the folders where these source files are found in the projects options and make sure the file test.c actually and really belongs to the project…


Logged

architect with some spare time  —  c::b compiled from last svn  —   openSuSE leap x86_64  —  AMD FX-4100


To frithjofh:
Oh, I see. I have to make it all a part of the same Project first.

I haven’t been making projects up ’til now. Just using single files and connecting them via the header-file.
Using a project solved my problem. Thank you. =)

But I have another question: if I want to send these files to a friend. Should I just send my entire folder with all files. Or just the project file. Or the project file + the .c and .h files?
Also: does my project (.cbp) work in other IDEs? (Such as Xcode on MAC)


Logged


But I have another question: if I want to send these files to a friend. Should I just send my entire folder with all files. Or just the project file. Or the project file + the .c and .h files?

You can try «Project ->  Create package for distribution», that should create a zip-file with the same basename as trhe project in the projects folder.
You might need a commandline zip.exe in the search path (http://wiki.codeblocks.org/index.php/Installing_Code::Blocks_from_source_on_Windows#ZIP).


Logged


I can’t find «Create package for distribution» under «Project» (or any other menu for that matter.)


Logged


I can’t find «Create package for distribution» under «Project» (or any other menu for that matter.)

Look into «Settings -> Scripting» and try to enable the «make_dist.script».


Logged


That worked.

Now I’ve made a .zip file out of my project. (Including all of my .c files, my .h file and the .cbp file)
If I send this .zip file to a friend and they unpack it. -Will they be able to open it with another IDE?


Logged


Some IDEs support other IDEs’ project formats, but this isn’t guaranteed.  They may have to create it themselves, using the source and header files as they are.


Logged


When I am trying to compile the following code,

int delayTime = 1;
int charBreak = 2.1;
#include <MemoryFree.h>

int rled1 = 1;
int rled2 = 2;
int rled3 = 3;
int rled4 = 4;
int rled5 = 5;
int gled1 = 6;
int gled2 = 7;
int gled3 = 8;
int gled4 = 9;
int gled5 = 10;
int bled1 = 11;
int bled2 = 12;
int bled3 = 13;
int bled4 = 14;
int bled5 = 15;

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

int ra[] = {4,288,18464,288,4};
int ga[] = {2,144,9232,144,2};
int ba[] = {1,72,4616,72,2};
int rb[] = {18724,16644,16644,2080,0};
int gb[] = {9362,8322,8322,1040,0};
int bb[] = {4681,4161,4161,520,0};
int rc[] = {2336,16388,16388,2080,0};
int gc[] = {1168,8194,8194,1040,0};
int bc[] = {584,4097,4097,520,0};
int rd[] = {18724,16388,16388,2336,0};
int gd[] = {9362,8194,8194,1168,0};
int bd[] = {4681,4097,4097,584,0};
int re[] = {18724,16644,16644,16388,0};
int ge[] = {9362,8322,8322,8194,0};
int be[] = {4681,4161,4161,4097,0};
int rf[] = {18724,16640,16640,16384,0};
int gf[] = {9362,8320,8320,8192,0};
int bf[] = {4681,4160,4160,4096,0};
int rg[] = {2336,16388,16420,16416,2084};
int gg[] = {1168,8194,8210,8208,1042};
int bg[] = {584,4097,4105,4104,521};
int rh[] = {18724,256,256,256,18724};
int gh[] = {9362,128,128,128,9362};
int bh[] = {4681,64,64,64,4681};
int ri[] = {0,16388,18724,16388,0};
int gi[] = {0,8194,9362,8194,0};
int bi[] = {0,4097,4681,4097,0};
int rj[] = {32,4,16388,18720,16384};
int gj[] = {16,2,8194,9360,8192};
int bj[] = {8,1,4097,4680,4096};
int rk[] = {18724,256,2080,16388,0};
int gk[] = {9362,128,1040,8194,0};
int bk[] = {4681,64,520,4097,0};
int rl[] = {18724,4,4,4,0};
int gm[] = {9362,2,2,2,0};
int bm[] = {4681,1,1,1,0};
int rm[] = {18724,2304,36,2304,18724};
int gm[] = {9362,1152,18,1152,9362};
int bm[] = {4681,576,9,576,4681};
int rn[] = {18724,2304,36,18724,0};
int gn[] = {9362,1152,18,9362,0};
int bn[] = {4681,576,9,4681,0};
int ro[] = {2336,16388,16388,2336,0};
int go[] = {1168,8194,8194,1168,0};
int bo[] = {584,4097,4097,584,0};
int rp[] = {18724,16640,16640,2048,0};
int gp[] = {9362,8320,8320,1024,0};
int bp[] = {4681,4160,4160,512,0};
int rq[] = {2336,16388,16420,2336,36};
int gq[] = {1168,8194,8210,1168,18};
int bq[] = {584,4097,4105,584,9};
int rr[] = {18724,16640,16672,2052,0};
int gr[] = {9362,8320,8336,1026,0};
int br[] = {4681,4160,4168,513,0};
int rs[] = {2048,16644,16644,32,0};
int gs[] = {1024,8322,8322,16,0};
int bs[] = {512,4161,4161,8,0};
int rt[] = {16384,16384,18724,16384,16384};
int gt[] = {8192,8192,9362,8192,8192};
int bt[] = {4096,4096,4681,4096,4096};
int ru[] = {18720,4,4,18720,0};
int gu[] = {9360,2,2,9360,0};
int bu[] = {4680,1,1,4680,0};
int rv[] = {18432,288,4,288,18432};
int gv[] = {9216,144,2,144,9216};
int bv[] = {4608,72,1,72,4608};
int rw[] = {18688,36,2304,36,18688};
int gw[] = {9344,18,1152,18,9344};
int bw[] = {4672,9,576,9,4672};
int rx[] = {16388,2080,256,2080,16388};
int gx[] = {8194,1040,128,1040,8194};
int bx[] = {4097,520,64,520,4097};
int ry[] = {16388,2080,256,2048,16384};
int gy[] = {8194,1040,128,1024,8192};
int by[] = {4097,520,64,512,4096};
int rz[] = {16420,16644,16644,18436,0};
int gz[] = {8210,8322,8322,9218,0};
int bz[] = {4105,4161,4161,4609,0};
int reos[] = {0,4,0,0,0};
int geos[] = {0,2,0,0,0};
int beos[] = {0,1,0,0,0};
int rque[] = {2048,16420,16640,2048,0};
int gque[] = {1024,8210,8320,1024,0};
int bque[] = {512,4105,4160,512,0};
int rexcl[] = {0,18692,0,0,0};
int gexcl[] = {0,9346,0,0,0};
int bexcl[] = {0,4673,0,0,0};

void displayLine(int line)
{
  int myline; myline = line;
  if (myline>=16384) {digitalWrite(rled1, HIGH); myline-=16384;} else {digitalWrite(rled1, LOW);}
  if (myline>=8192) {digitalWrite(gled1, HIGH); myline-=8192;} else {digitalWrite(gled1, LOW);}
  if (myline>=4096) {digitalWrite(bled1, HIGH); myline-=4096;} else {digitalWrite(bled1, LOW);}
  if (myline>=2048) {digitalWrite(rled2, HIGH); myline-=2048;} else {digitalWrite(rled2, LOW);}
  if (myline>=1024) {digitalWrite(gled2, HIGH); myline-=1024;} else {digitalWrite(gled2, LOW);}
  if (myline>=512) {digitalWrite(bled2, HIGH); myline-=512;} else {digitalWrite(bled2, LOW);}
  if (myline>=256) {digitalWrite(rled3, HIGH); myline-=256;} else {digitalWrite(rled3, LOW);}
  if (myline>=128) {digitalWrite(gled3, HIGH); myline-=128;} else {digitalWrite(gled3, LOW);}
  if (myline>=64) {digitalWrite(bled3, HIGH); myline-=64;} else {digitalWrite(bled3, LOW);}
  if (myline>=32) {digitalWrite(rled4, HIGH); myline-=32;} else {digitalWrite(rled4, LOW);}
  if (myline>=16) {digitalWrite(gled4, HIGH); myline-=16;} else {digitalWrite(gled4, LOW);}
  if (myline>=8) {digitalWrite(bled4, HIGH); myline-=8;} else {digitalWrite(bled4, LOW);}
  if (myline>=4) {digitalWrite(rled5, HIGH); myline-=4;} else {digitalWrite(rled5, LOW);}
  if (myline>=2) {digitalWrite(gled5, HIGH); myline-=2;} else {digitalWrite(gled5, LOW);}
  if (myline>=1) {digitalWrite(bled5, HIGH); myline-=1;} else {digitalWrite(bled5, LOW);}

}

void displayChar(char c)
{
  if (c == 'ra'){for (int i = 0; i <5; i++){displayLine(ra[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rb'){for (int i = 0; i <5; i++){displayLine(rb[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rc'){for (int i = 0; i <5; i++){displayLine(rc2[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rd'){for (int i = 0; i <5; i++){displayLine(rd[i]);delay(delayTime);}displayLine(0);}
  if (c == 're'){for (int i = 0; i <5; i++){displayLine(re[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rf'){for (int i = 0; i <5; i++){displayLine(rf[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rg'){for (int i = 0; i <5; i++){displayLine(rg[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rh'){for (int i = 0; i <5; i++){displayLine(rh[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ri'){for (int it = 0; it <5; it++){displayLine(ri[it]);delay(delayTime);}displayLine(0);}
  if (c == 'rj'){for (int i = 0; i <5; i++){displayLine(rj[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rk'){for (int i = 0; i <5; i++){displayLine(rk[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rl'){for (int i = 0; i <5; i++){displayLine(rl[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rm'){for (int i = 0; i <5; i++){displayLine(rm[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rn'){for (int i = 0; i <5; i++){displayLine(rn[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ro'){for (int i = 0; i <5; i++){displayLine(ro[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rp'){for (int i = 0; i <5; i++){displayLine(rp[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rq'){for (int i = 0; i <5; i++){displayLine(rq[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rr'){for (int i = 0; i <5; i++){displayLine(rr[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rs'){for (int i = 0; i <5; i++){displayLine(rs[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rt'){for (int i = 0; i <5; i++){displayLine(rt[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ru'){for (int i = 0; i <5; i++){displayLine(ru[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rv'){for (int i = 0; i <5; i++){displayLine(rv[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rw'){for (int i = 0; i <5; i++){displayLine(rw[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rx'){for (int i = 0; i <5; i++){displayLine(rx[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ry'){for (int i = 0; i <5; i++){displayLine(ry[i]);delay(delayTime);}displayLine(0);}
  if (c == 'rz'){for (int i = 0; i <5; i++){displayLine(rz[i]);delay(delayTime);}displayLine(0);}
  if (c == 'r!'){for (int i = 0; i <5; i++){displayLine(rexcl[i]);delay(delayTime);}displayLine(0);}
  if (c == 'r?'){for (int i = 0; i <5; i++){displayLine(rques[i]);delay(delayTime);}displayLine(0);}
  if (c == 'r.'){for (int i = 0; i <5; i++){displayLine(reos[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ga'){for (int i = 0; i <5; i++){displayLine(ga[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gb'){for (int i = 0; i <5; i++){displayLine(gb[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gc'){for (int i = 0; i <5; i++){displayLine(gc2[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gd'){for (int i = 0; i <5; i++){displayLine(gd[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ge'){for (int i = 0; i <5; i++){displayLine(ge[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gf'){for (int i = 0; i <5; i++){displayLine(gf[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gg'){for (int i = 0; i <5; i++){displayLine(gg[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gh'){for (int i = 0; i <5; i++){displayLine(gh[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gi'){for (int it = 0; it <5; it++){displayLine(gi[it]);delay(delayTime);}displayLine(0);}
  if (c == 'gj'){for (int i = 0; i <5; i++){displayLine(gj[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gk'){for (int i = 0; i <5; i++){displayLine(gk[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gl'){for (int i = 0; i <5; i++){displayLine(gl[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gm'){for (int i = 0; i <5; i++){displayLine(gm[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gn'){for (int i = 0; i <5; i++){displayLine(gn[i]);delay(delayTime);}displayLine(0);}
  if (c == 'go'){for (int i = 0; i <5; i++){displayLine(go[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gp'){for (int i = 0; i <5; i++){displayLine(gp[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gq'){for (int i = 0; i <5; i++){displayLine(gq[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gr'){for (int i = 0; i <5; i++){displayLine(gr[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gs'){for (int i = 0; i <5; i++){displayLine(gs[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gt'){for (int i = 0; i <5; i++){displayLine(gt[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gu'){for (int i = 0; i <5; i++){displayLine(gu[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gv'){for (int i = 0; i <5; i++){displayLine(gv[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gw'){for (int i = 0; i <5; i++){displayLine(gw[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gx'){for (int i = 0; i <5; i++){displayLine(gx[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gy'){for (int i = 0; i <5; i++){displayLine(gy[i]);delay(delayTime);}displayLine(0);}
  if (c == 'gz'){for (int i = 0; i <5; i++){displayLine(gz[i]);delay(delayTime);}displayLine(0);}
  if (c == 'g!'){for (int i = 0; i <5; i++){displayLine(gexcl[i]);delay(delayTime);}displayLine(0);}
  if (c == 'g?'){for (int i = 0; i <5; i++){displayLine(gques[i]);delay(delayTime);}displayLine(0);}
  if (c == 'g.'){for (int i = 0; i <5; i++){displayLine(geos[i]);delay(delayTime);}displayLine(0);}
  if (c == 'ba'){for (int i = 0; i <5; i++){displayLine(ba[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bb'){for (int i = 0; i <5; i++){displayLine(bb[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bc'){for (int i = 0; i <5; i++){displayLine(bc2[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bd'){for (int i = 0; i <5; i++){displayLine(bd[i]);delay(delayTime);}displayLine(0);}
  if (c == 'be'){for (int i = 0; i <5; i++){displayLine(be[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bf'){for (int i = 0; i <5; i++){displayLine(bf[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bg'){for (int i = 0; i <5; i++){displayLine(bg[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bh'){for (int i = 0; i <5; i++){displayLine(bh[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bi'){for (int it = 0; it <5; it++){displayLine(bi[it]);delay(delayTime);}displayLine(0);}
  if (c == 'bj'){for (int i = 0; i <5; i++){displayLine(bj[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bk'){for (int i = 0; i <5; i++){displayLine(bk[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bl'){for (int i = 0; i <5; i++){displayLine(bl[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bm'){for (int i = 0; i <5; i++){displayLine(bm[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bn'){for (int i = 0; i <5; i++){displayLine(bn[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bo'){for (int i = 0; i <5; i++){displayLine(bo[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bp'){for (int i = 0; i <5; i++){displayLine(bp[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bq'){for (int i = 0; i <5; i++){displayLine(bq[i]);delay(delayTime);}displayLine(0);}
  if (c == 'br'){for (int i = 0; i <5; i++){displayLine(br[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bs'){for (int i = 0; i <5; i++){displayLine(bs[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bt'){for (int i = 0; i <5; i++){displayLine(bt[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bu'){for (int i = 0; i <5; i++){displayLine(bu[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bv'){for (int i = 0; i <5; i++){displayLine(bv[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bw'){for (int i = 0; i <5; i++){displayLine(bw[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bx'){for (int i = 0; i <5; i++){displayLine(bx[i]);delay(delayTime);}displayLine(0);}
  if (c == 'by'){for (int i = 0; i <5; i++){displayLine(by[i]);delay(delayTime);}displayLine(0);}
  if (c == 'bz'){for (int i = 0; i <5; i++){displayLine(bz[i]);delay(delayTime);}displayLine(0);}
  if (c == 'b!'){for (int i = 0; i <5; i++){displayLine(bexcl[i]);delay(delayTime);}displayLine(0);}
  if (c == 'b?'){for (int i = 0; i <5; i++){displayLine(bques[i]);delay(delayTime);}displayLine(0);}
  if (c == 'b.'){for (int i = 0; i <5; i++){displayLine(beos[i]);delay(delayTime);}displayLine(0);}
  delay(charBreak);
}

void displayString(char* s)
{
  for (int i = 0; i<=strlen(s); i++)
  {
  displayChar(s[i]);
  }
}

void loop()
{
  displayString("sunil");
  Serial.print("freeMemory()=");
    Serial.println(freeMemory());
}

I am getting the following error.

sketch_jul17b:63: error: redefinition of 'int gm []'
sketch_jul17b:60: error: 'int gm [5]' previously defined here
sketch_jul17b:64: error: redefinition of 'int bm []'
sketch_jul17b:61: error: 'int bm [5]' previously defined here
sketch_jul17b.ino: In function 'void displayChar(char)':
sketch_jul17b:139: error: 'rc2' was not declared in this scope
sketch_jul17b:164: error: 'rques' was not declared in this scope
sketch_jul17b:168: error: 'gc2' was not declared in this scope
sketch_jul17b:177: error: 'gl' was not declared in this scope
sketch_jul17b:193: error: 'gques' was not declared in this scope
sketch_jul17b:197: error: 'bc2' was not declared in this scope
sketch_jul17b:206: error: 'bl' was not declared in this scope
sketch_jul17b:222: error: 'bques' was not declared in this scope

I don’t know why this error is occurring.

Синтаксические ошибки

Первые ошибки, которые определяются отладчиком – это синтаксические ошибки. Их же легче всего исправить. Неправильный синтаксис в Arduino IDE выделяется строкой, в которой допущена неточность. Нужно разобраться – это ошибка в написании служебного слова, случайно удалена важная функция, не хватает закрывающейся скобки или неправильно отделены комментарии.

Для определения ошибки внимательно просмотрите строку-подсказку и внесите необходимые изменения. Ниже мы приведем примеры наиболее часто встречающихся синтаксических ошибок компиляции кода:

  • Ошибка “expected initializer before ‘}’ token” говорит о том, что случайно удалена или не открыта фигурная скобка.
  • Ошибка “a function-definition is not allowed here before ‘{‘ token” – аналогичная предыдущей и указывает на отсутствие открывающейся скобки, например, открывающих скобок в скетче только 11, а закрывающих 12.
  • Уведомление об ошибке “undefined reference to “setup” получите в случае переименования или удаления функции “setup”.
  • Ошибка “undefined reference to “loop” – возникает в случае удаления функции loop. Без команд этой функции компилятор запустить программу не сможет. Для устранения надо вернуть каждую из команд на нужное место в скетче.
  • Ошибка “… was not declared in this scope” обозначает, что в программном коде обнаружены слова, которые написаны с ошибкой (например, которые обозначают какую-то функцию) или найдены необъявленные переменные, методы. Подобная ошибка возникает также в случае случайного удаления значка комментариев и текст, который не должен восприниматься как программа, читается IDE.

Ошибки компиляции и их решения, для плат Arduino, синтаксические ошибки картинка

Ошибки библиотек

Большое количество ошибок возникает на уровне подключения библиотек или неправильного их функционирования. Наиболее известные:

  • “fatal error: … No such file or directory”. Такое сообщение вы получите, если необходимую в скетче библиотеку вы не записали в папку libraries. Сообщение об ошибке в одном из подключенных файлов может означать, что вы используете библиотеку с ошибками или библиотеки не совместимы. Решение – обратиться к разработчику библиотеки или еще раз проверить правильность написанной вами структуры.
  • “redefinition of void setup” – сообщение возникает, если автор библиотеки объявил функции, которые используются и в вашем коде. Чтобы исправить – переименуйте свои методы или в библиотеке.

Ошибки компилятора

Нестабильность в работе самого компилятора тоже могут возникать при отладке программы. Вариантов выхода из данной ситуации может быть несколько, например, установить последнюю версию компилятора. Иногда решением может быть наоборот, возвращение до более старой версии. Тогда используемая библиотека может работать корректно.

Ошибки компиляции при работе с разными платами — Uno, Mega и Nano

В Arduino можно писать программы под разные варианты микроконтроллеров. По умолчанию в меню выбрана плата Arduino/Genuino Uno. Если забудете о том что нужно указать нужную плату – в вашем коде будут ссылки на методы или переменные, не описанные в конфигурации “по умолчанию”.

Вы получите ошибку при компиляции “programmer is not responding”. Чтобы исправить ее – проверьте правильность написания кода в части выбора портов и вида платы. Для этого в Ардуино IDE в меню «Сервис» выберите плату. Аналогично укажите порт в меню “Сервис” – пункт «Последовательный порт».

Ошибка exit status 1

В среде разработки такое сообщение можно увидеть во многих случаях. И хотя в документации данная ошибка указывается как причина невозможности запуска IDE Аrduino в нужной конфигурации, на самом деле причины могут быть и другие. Для того, чтобы найти место, где скрывается эта ошибка можно “перелопатить” действительно много. Но все же стоит сначала проверить разрядность системы и доступные библиотеки.

Ошибки компиляции и их решения, для плат Arduino, Ошибка exit status 1

Обновления и исправления касательно версий инструкции и ПО

Понравилась статья? Поделить с друзьями:
  • Redefinition of error xcode
  • Redefinition of class c как исправить
  • Reddragon mitra как изменить подсветку
  • Red4ext cyberpunk 2077 ошибка при запуске
  • Red state asus как исправить