2386 views|9 replies

2870

Posts

4

Resources
The OP
 

Strange problem about Keil MDK 6 and 5 compilers [Copy link]

After I upgraded the old version of the project to the new version, I found that the program could not be executed smoothly. After looking for the reason, it turned out to be caused by the different versions of the MKD compiler used. The old version was compiled with MDK 5 and worked properly. The new version was compiled with MDK 6, but it did not work properly. The old version was compiled with MDK 6 and it also did not work properly.

By finding out that the problem lies in the function pointer, I wrote the following verification program segment to reproduce the problem.

//声明代码
#define inx_1st  0
typedef uint16_t (*fun_cmd)(uint16_t,uint16_t);

const fun_cmd  cmd_list[]={cmd1,cmd2}

//调用代码
fun_cmd  cmd_task = NULL;

cmd_task = cmd_list[inx_1st];//动态的调用,由程序执行决定
result = cmd_task(0,0); //MKD 6 编译无法调用成功


//函数声明
/*  none cmmand */
uint16_t cmd1(uint16_t x,uint16_t y)
{
	g_val = 1; 	
	return 0;
}

/*  none cmmand */
uint16_t cmd2(uint16_t x,uint16_t y)
{
    g_val = 2; 	
	return 0;
}



This program can be compiled and executed regardless of MKD 5 or 6, but it cannot work with version 6. It is also impossible to interrupt execution during debugging. I feel that version 6 of MDK seems to have optimized the program body and simply returns without executing the contents of the program body. Since it can be compiled and executed, I don't know what is going on. Is there something wrong with my declaration somewhere?

This post is from Programming Basics

Latest reply

It is possible, these are all things that can be evaluated statically. Try turning off optimization.   Details Published on 2024-1-30 19:51
 

6787

Posts

2

Resources
2
 
Just use whichever version of the compiler is stable. You don't know how the compiler works internally.
This post is from Programming Basics

Comments

I mainly want to know whether my program code is standardized. If the problem is caused by writing C code in an unstandardized way, I will study hard.  Details Published on 2024-1-26 13:40
 
 
 

2870

Posts

4

Resources
3
 
wangerxian posted on 2024-1-26 13:14 Use the compiler that is stable. You don't know how the compiler works internally.

I mainly want to know whether my program code is standardized. If the problem is caused by writing C code in an unstandardized way, I will study hard.

This post is from Programming Basics

Comments

I see no problem with the code, except that the function pointer is called. Is it because MDK 6 has higher requirements for function pointer calls?  Details Published on 2024-1-26 15:15
 
 
 

6787

Posts

2

Resources
4
 
bigbat posted on 2024-1-26 13:40 I mainly want to know whether my program code is standardized. If the problem is caused by writing C code in an unstandardized way, I will study hard

I see no problem with the code, except that the function pointer is called. Is it because MDK 6 has higher requirements for function pointer calls?

This post is from Programming Basics
 
 
 

7462

Posts

2

Resources
5
 

uint16_t cmd1(uint16_t x,uint16_t y); Try adding a declaration in front

This post is from Programming Basics
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

7462

Posts

2

Resources
6
 

Or add static

This post is from Programming Basics

Comments

I'm surprised that it can be compiled, which means there should be no problem with the syntax. I feel like it's an optimization problem.  Details Published on 2024-1-30 17:29
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

2870

Posts

4

Resources
7
 

I'm surprised that it can be compiled, which means there should be no problem with the syntax. I feel like it's an optimization problem.

This post is from Programming Basics

Comments

It depends on your optimization level. You can try to adjust it without optimization.  Details Published on 2024-1-30 19:51
It depends on your optimization level. You can try to adjust it without optimization.  Details Published on 2024-1-30 19:42
 
 
 

114

Posts

6

Resources
8
 
bigbat posted on 2024-1-30 17:29 I am surprised that it can be compiled, which means there should be no problem with the syntax. I feel like it is an optimization problem.

It depends on your optimization level. You can try to adjust it without optimization.

This post is from Programming Basics
 
 
 

7462

Posts

2

Resources
9
 
bigbat posted on 2024-1-30 17:29 I am surprised that it can be compiled, which means there should be no problem with the syntax. I feel like it is an optimization problem.

It is possible, these are all things that can be evaluated statically. Try turning off optimization.

This post is from Programming Basics
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

2870

Posts

4

Resources
10
 

The problem has been clarified. It is caused by optimization. It can be avoided by reducing the optimization to -O2. The default is -O3, the highest optimization level.

This post is from Programming Basics
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list