| Author |
Message |
|
|
Posted: Mar 28, 2009 - 08:35 AM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
|
Quote:
main c file
Why the need for a main C file? As you can't create and use C++ objects from C code this will add an extra layer of calls to the code.
The somewhat simplified truth is that C++ can call C but not the other way around. The slightly more complicated version is: If your main is in a C source file it has "C linkage". It can call a function in a C++ file but that called function also has to have C linkage (which you get by using the extern "C" language construct). This function in turn can create and use C++ objects.
I seems you want a small C/C++ demo, and having a main in C rather than C++ only makes that demo more complicated.
Could you do with a project with a main function in a C++ file? |
|
|
| |
|
|
|
|
|
Posted: Mar 29, 2009 - 07:21 AM |
|


Joined: Dec 22, 2005
Posts: 1261
Location: shiraz , iran
|
|
| i mean the c++ main file sorry for that.all i want is a simple c++ demo. |
_________________ I love Digital
and you who involved in it!
|
| |
|
|
|
|
|
Posted: Mar 30, 2009 - 02:45 AM |
|

Joined: Sep 07, 2004
Posts: 2528
Location: New York State
|
|
| This will blink the LED on my Olimex P40 board. |
Last edited by steve17 on Mar 31, 2009 - 11:48 PM; edited 1 time in total
|
| |
|
|
|
|
|
Posted: Mar 31, 2009 - 07:04 AM |
|


Joined: Dec 22, 2005
Posts: 1261
Location: shiraz , iran
|
|
| thnaks a lot! |
_________________ I love Digital
and you who involved in it!
|
| |
|
|
|
|
|
Posted: May 04, 2009 - 06:35 PM |
|

Joined: May 04, 2009
Posts: 7
|
|
hey.. i've got a cuestion.. does the c++ compiler support the implicit convertions for integrated types like strings, integers, etc..? For example, when a function argument should be a string, and i pass it a integer, can the convertion be done by the compiler?
Thanks |
|
|
| |
|
|
|
|
|
Posted: May 04, 2009 - 09:21 PM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
|
Quote:
For example, when a function argument should be a string, and i pass it a integer, can the convertion be done by the compiler?
Assuming that you by string mean a char * then the answer is no. This is not a pecularity of the avr-gcc compiler. This is how the C++ language is defined. |
|
|
| |
|
|
|
|
|
Posted: May 05, 2009 - 07:32 PM |
|

Joined: May 04, 2009
Posts: 7
|
|
| [quote="JohanEkdahl"]
Quote:
Assuming that you by string mean a char * then the answer is no. This is not a pecularity of the avr-gcc compiler. This is how the C++ language is defined.
Thanks for the answer.. I'm using the c++.exe file to compile my program instead of avr-gcc.exe (just like TFrancuz says at the fist comment)so it's c++ language(i guess.. ), and it still doesn't work.. so, is there a way to make it able to work with implicit convertions?
..thanks |
|
|
| |
|
|
|
|
|
Posted: May 05, 2009 - 08:09 PM |
|

Joined: May 04, 2009
Posts: 7
|
|
| hey.. has anybody had a problem with strings (char *)? 'cause i'm trying strlen, strcat and itoa.. and they just dont work!!!! maybe there's something i'm doing wrong |
|
|
| |
|
|
|
|
|
Posted: May 05, 2009 - 09:01 PM |
|


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
and they just dont work
Shows as small a program as you possibly can that demonstrates them not working as far as you are concerned. |
_________________
|
| |
|
|
|
|
|
Posted: May 05, 2009 - 09:18 PM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
|
Quote:
so it's c++ language(i guess.. Rolling Eyes ), and it still doesn't work..
It does not matter if it is C or C++. There is no implicit conversion from int to char * in neither language. What makes you think there is?
And AFAIK the language is determined by the filename extension of the source file. Not that it matters in this case, though. You can easily determine if the compiler thinks it is a C or a C++ file that you are sending to it. Just throw a very simple class into it, eg
Code:
class Test
{
public:
int i;
};
and if it barfs over that then it is C, not C++. There are other methods, including testing if CPLUSPLUS (or some such, I really don't recall and don't have the energy to search the correct preprocessor symbol name out) is defined. |
|
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 12:42 AM |
|


Joined: Nov 17, 2004
Posts: 6137
Location: Great Smokey Mountains.
|
|
Johan,
I want to drive my Jeep to Tahiti, but Jeeps get pretty poor gas mileage and I'm afraid I won't be able to make it from California to Hawaii so can you tell me how to flag down tankers along the way?
Smiley |
_________________ FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 06:55 AM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
I totally lost you there, Joe. Honestly. Knowing you (netwise), quite good I think, I suppose there is some really to-the-point wittiness or sarcasm somewhere in there regarding some mistake I made, but I cant spot it.
I have tried Googling "Smileys Jeep gas shortage bottom Pacific" with and without "driver nuts" but came up with nothing..
Help this stupid Swede out, and give me a clue. Please?! |
|
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 04:29 PM |
|


Joined: Nov 17, 2004
Posts: 6137
Location: Great Smokey Mountains.
|
|
Johan,
It wasn't aimed at you, but an attempt to wisecrack about getting into details like char * while using the wrong vehicle for the job in the first place. Some people want to use C++ as C without studying the basics or the philosophical differences between procedural versus object oriented programming. Not pointed at you, just a poor joke about other folks who want to use C++ when they don't seem to know anything about why one would choose C++ over C.
Sorry,
Smiley |
_________________ FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 05:14 PM |
|

Joined: May 04, 2009
Posts: 7
|
|
|
smileymicros wrote:
Johan,
It wasn't aimed at you, but an attempt to wisecrack about getting into details like char * while using the wrong vehicle for the job in the first place. Some people want to use C++ as C without studying the basics or the philosophical differences between procedural versus object oriented programming. Not pointed at you, just a poor joke about other folks who want to use C++ when they don't seem to know anything about why one would choose C++ over C.
Sorry,
Smiley
So.. i guess you're talking about me.. are not you? And i wonder, why does a philosopher (haha) say that other folks don't know anything about programming paradigms without asking them first?
Before you say anything, try to be sure... this is a tip for life |
|
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 05:24 PM |
|

Joined: May 04, 2009
Posts: 7
|
|
|
Quote:
Shows as small a program as you possibly can that demonstrates them not working as far as you are concerned.
i've overloaded the << operator to work with a class lcd and strings like: lcd<<"something";
in the function definition i've tried to use the strlen function like this:
Lcd & Lcd::operator<<(char *s)
{
...
int length = strlen(s);
...
}
and i use the variable length at a for loop to print the string.. but it does not make any loop |
|
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 08:00 PM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
OK, I'd actually like to see a bit more, eg the whole of the implementation of the operator overload, a declaration/creation of an Lcd object and a call/use of the operator.
I dont have AVR Studio handy right now (just taking a break with working overtime). But I did pull together this small test in MS VC++ which works like a charm. I of-course simulate sending the characters to the LCD proper with a call to putchar, but apart from that I suppose I am close to what you have?
Code:
// JSkywalker.cpp
#include "stdafx.h"
#include <string>
class Lcd
{
public:
Lcd & operator<<(char * s)
{
for (size_t i= 0; i<strlen(s); i++) {
putchar(s[i]);
}
return *this;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Lcd lcd;
lcd << "Hello overload";
return 0;
}
I will try something similar in AVR Studio when I get home, if I'm not too tired. |
|
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 08:10 PM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
|
Quote:
It wasn't aimed at you, but an attempt to wisecrack about getting into details like char * while using the wrong vehicle for the job in the first place.
That's okay, Joe. I wasn't upset but rather in wisecrack mode myself. Still, and you know this, my position is that many (dare I say "most"?) times C++ is just as good vehicle as C for embedded systems. We might then be in a situation where someone uses an appropriate vehicle but does not know why (bad), and maybe how to operate it (worse).
Please note that I am not accusing JSkywalker being in either situation, but merely dwelling on Joes scenario in general!
Quote:
Sorry
No need to be, Joe. If you actually succeed in hurting me for real, we will settle that in a PM or two. No need for old grumpy beards to fight such things out in public. Anfd you haven't even gotten close yet.  |
|
|
| |
|
|
|
|
|
Posted: May 06, 2009 - 09:48 PM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
Here's what what I did:
1) Put the Lcd class in a C++ file, and in that file also put a global function test() that exercises the overloaded operator and gave that function C linkage (so that it can be called from main() that is in a C file):
Code:
//JSkywalker.cpp
#include <avr/io.h>
#include <string.h>
class Lcd
{
public:
Lcd & operator<<(char * s)
{
for (size_t i= 0; i<strlen(s); i++) {
PORTB=s[i];
}
return *this;
}
};
extern "C" void test()
{
Lcd lcd;
lcd << (char *)"Hello overload";
}
2) wrote a simple main.c, containing main() that calls test(). No, I didnt bother to create a header file for the test() function. Call me sloppy if you like...
Code:
//main.c
#include <avr/io.h>
void test();
int main(void)
{
test();
return 0;
}
3) Fiddled a bit with the makefile before I got it right (it's been a while since my last experiments with C++ on AVRs).
4) Built the whole shebang, started up AVR Studio, loaded the ELF file and stepped through the code. I see it looping inside the overloaded Lcd::operator<< outputting the chars to PORTB one after the other.
A total success as far as I am concerned.
So now tell us in similar detail, and preferrably in a likewise minimal test/demo project, what your problem is. |
|
|
| |
|
|
|
|
|
Posted: May 07, 2009 - 01:04 AM |
|


Joined: Nov 17, 2004
Posts: 6137
Location: Great Smokey Mountains.
|
|
| Never mind |
_________________ FREE TUTORIAL: 'Quick Start Guide for Using the WinAVR C Compiler with ATMEL's AVR Butterfly' AVAILABLE AT: http://www.smileymicros.com
|
| |
|
|
|
|
|
Posted: May 07, 2009 - 09:29 PM |
|


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden
|
|
|
|
|
|
|