Error in building the project in code blocks [closed] - project

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I Have included all the files in the codeblocks while making simple cosole C Application but this Error come when i define the macro. i guess linker is not able to link all files in project .i have two files included in project test1.c and test1.h code in files is as shown below..
file :::::test1.c
#include<stdio.h>
void m();
#include "test1.h"
#define DS==1
int main(){
return 0;
}
file::::test1.h
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
#if DS ==1
void m(){
printf("hello DS==1");
}
#eliif DS==2
void main(){
printf("hello DS==2");
}
#endif
#endif // TEST1_H_INCLUDED
Error is that
**> E:\Documents\Myprojects\My C PRoj\Te\test1.c||In function 'main':|
E:\Documents\Myprojects\My C PRoj\Te\test1.c|8|warning: implicit declaration of function 'm'|
obj\Debug\test1.o||In function `main':|
E:\Documents\Myprojects\My C PRoj\Te\test1.c|8|undefined reference to `m'|
||=== Build finished: 1 errors, 1 warnings ===|**
if i remove the conditional macro an compile simply with following code:
file:::::test1.c
#include<stdio.h>
#include "test1.h"
int main(){
m();
return 0;
}
file:::::test1.h
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
void m(){
printf("uncoditional macro");
}
#endif // TEST1_H_INCLUDED
every thing works fine .what is reason for that?

The expression ++a+( a+b) is undefined behavior because a sequence point is missing between the update of a and its use. The output can be anything.

Related

How can i use a Library for another Library ? [Arduino ESP32] [duplicate]

This question already has answers here:
C++ Global variable declaration
(5 answers)
Closed 9 months ago.
I am working on a project on Arduino ESP32 and I have a lot of Global variables (for data generation). I have decided to create a library in order to orgenise my work a little better. But I use this library into other librari's that I had to create for other usage. after compilation it I have the following error :
sketch\OX2inj_LEVEL_OX2.cpp.o:(.data.addrChipId+0x0): multiple definition of `addrChipId'
sketch\First_Useage.cpp.o:(.data.addrChipId+0x0): first defined here
sketch\OX2inj_LEVEL_OX2.cpp.o:(.bss.ChipID+0x0): multiple definition of `ChipID'
sketch\First_Useage.cpp.o:(.bss.ChipID+0x0): first defined here
here is my .ino (main) code :
#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
#include "First_Useage.h"
//---------somthing
void setup()
{
Serial.begin(115200);
//---------somthing
Serial.println(ChipID.ReadStrEEPROM());
//---------somthing
}
void loop()
{
//---------somthing
}
here is my "Def_Global_Var.h" code
#ifndef Def_Global_Var_H
#define Def_Global_Var_H
#include "Var_Str_EEPROM.h"
uint16_t addrChipId = 1;
VarStrEEPROM ChipID(addrChipId);
#endif
here is my "First_Useage.h" code
#ifndef First_Usage_H
#define First_Usage_H
void getchipid();
#endif
here is my "First_Useage.cpp" code :
#include "First_Useage.h"
#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
void getchipid()
{
uint32_t chipId = 0;
for(int i=0; i<17; i=i+8)
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
ChipID.WriteStrEEPROM(String(chipId));
}
My understanding is that, when I use the #include "Def_Global_Var.h", the programme thinks that : "I am calling the library" and it sees that it has been called before and it does not like it.
Is it somehow correct ? and if it is(or not) correct what should I do?
EDIT : sorry I have put the wrong part of the prog. it has been corrected now
The actual cause is that the header is included into several source files, so you end up with multiple conflicting definitions of these variables in your .o files.
You shouldn't normally define global variables in header files at all; you should only declare them as extern:
#ifndef Def_Global_Var_H
#define Def_Global_Var_H
...
extern uint16_t addrChipId;
...
#endif
The second step is to define the variable in the corresponding .cpp file, this time without the extern keyword:
// Def_Global_Var.cpp
uint16_t addrChipId = 1;
Since Def_Global_Var.o gets linked only once, there should be no more conflicts.

Static declaration of '__vector_1' follows non-static declaration

Im trying to create a program which will interrupt when I press the button. I have Atmega8 and I use Microchip studio for coding.
I checked the document about interrupts on atmega's website however I can't say I totally got it.
Here is my code:
#define F_CPU 1000000UL
#define IRQ1 INT0_vect
#define IRQ2 INT1_vect
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void init()
{
DDRB=0b11111111;
PORTB=255;
_delay_ms(2000);
PORTB=0;
DDRD = 0b00000000;
GICR=0xc0;
MCUCR=0x08;
}
int main(void){
init();
volatile int mode = 0;
ISR(IRQ1){
_delay_ms(500);
if (mode<3)mode++; else mode = 0;
}
ISR(IRQ2){
_delay_ms(150);
}
}
Errors I get:
Imgur
I would be glad if any admin edits my question and add picture here, website doesn't let me add photo because I need at least 10 reputation to post image
Don't try to define functions inside of other functions unless you really know what you are doing. You should move the ISR definitions to the top level of the file, putting them outside of main.

Generate random identifier in C Preprocessor to avoid duplicate linker symbols

I'm trying to solve Can Xcode tell me if I forget to include a category implementation in my target?, and I came up with the following solution:
NSObject+Foo.h
extern int volatile canary;
void canaryCage() {
canary = 0;
}
NSObject+Foo.m
int canary = 0;
Now, if I #import "NSObject+Foo.h" in a source file, I'll get a linker error if that NSObject+Foo.m wasn't also included in my target.
However, every time I #import "NSObject+Foo.h" I generate a duplicate _canaryCage symbol. I can't use __COUNTER__ because I only #import "NSObject+Foo.h" in implementation files. I need canaryCage to be unique across my whole symbol table.
I need something like:
#define CONCAT(x, y) x##y
#define CONCAT2(x, y) CONCAT(x, y)
extern int volatile canary;
void CONCAT2(canaryCage, __RANDOM__)() {
canary = 0;
}
This way, if I have source files like:
Bar.m
#import "NSObject+Foo.h"
Baz.m
#import "NSObject+Foo.h"
I'll get symbols like _canaryCage9572098740753234521 and _canaryCage549569815492345, which won't conflict. I also don't want to enable --allow-multiple-definition in ld because I want other duplicate symbol definitions to cause an error. I don't want to use canaryCage for anything but a marker that I forgot to link a source file whose header I #imported.
If you make it static, each translation unit will get its own copy, and everything else should work the way you want it to - no preprocessor gymnastics required.
static void canaryCage()
{
canary = 0;
}
This answer was close, but it resulted in canaryCage being optimized away because it was dead code.
Solution:
NSObject+Foo.h
extern int canary;
__attribute__((constructor)) static void canaryCage() {
canary = 0;
}
NSObject+Foo.m
int canary = 0;
Unfortunately, this adds some overhead every time the category is imported, but the overhead is very minimal. If anyone knows a way to prevent canaryCage from being stripped, I'll happily mark their answer as correct.

How to make a loadable dll to use tcl code functionality by any program

I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.
Here is my simple tcl code :
/* Filename : simple_addition.tcl */
#!/usr/bin/env tclsh8.5
proc add_two_nos { } {
set a 10
set b 20
set c [expr { $a + $b } ]
puts " c is $c ......."
}
And here is my c wrapper function which uses the above tcl addition functionality :
#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;
printf("inside main function \n");
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");
/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));
/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
fprintf(stderr, "ERROR in script: %s\n", result);
exit(1);
}
/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
printf("%s\n", result);
}
/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
}
This c wrapper is working fine for me and gives correct results.
Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.
In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.
VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
Tcl_FindExecutable(NULL);
interp = Tcl_CreateInterp();
}
case DLL_THREAD_ATTACH:
break ;
case DLL_THREAD_DETACH:
break ;
case DLL_PROCESS_DETACH:
{
Tcl_DeleteInterp(interp);
break;
}
}
return TRUE;
}
and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.

Separating Code To Be Organized

I'm building a zipper application, but it has a declaration that I want to separate it in another file (compress-file.m), but only when I separate the files I got an error when compiling with a variable, see it:
[ubuntu#eeepc:~/Desktop] make
This is gnustep-make 2.0.2. Type 'make print-gnustep-make-help' for help.
Making all for app LeafZip...
Creating LeafZip.app/....
Compiling file main.m ...
main.m: In function ‘main’:
main.m:7: error: ‘PATH_MAX’ undeclared (first use in this function)
main.m:7: error: (Each undeclared identifier is reported only once
main.m:7: error: for each function it appears in.)
main.m:12: warning: implicit declaration of function ‘compressFile’
main.m:7: warning: unused variable ‘outFileName’
make[1]: *** [obj/main.o] Error 1
make: *** [LeafZip.all.app.variables] Error 2
Also see the line 7 of main.m file:
char outFileName[PATH_MAX] = { 0 };
And see some lines of compress-file.m:
#include <stdio.h>
#include <zlib.h>
#include <limits.h>
/* Buffer to hold data read */
char buf[BUFSIZ] = { 0 };
size_t bytes_read = 0;
gzFile *out = gzopen(outFileName, "wb");
I know that is Objective-C extension, but it's only because when I solve this problem I will need to continue the development in Objective-C. What I need to do to correct this?
PATH_MAX is not always defined by including <limits.h>. If you want to use it, you probably need to fall back on the fragment:
#include <limits.h>
#ifndef PATH_MAX
#define PATH_MAX _POSIX_PATH_MAX /* Or possibly _XOPEN_PATH_MAX */
#endif /* PATH_MAX */
Did you even include limits.h in your main program? If not, you need to do so.
Looks like main.m needs to #include <limits.h>. It also seems like it will need to include a header describing compressFile (which I guess you moved into compress-file.m.