關於我自己

2017年9月23日 星期六

C static function/static inline

測試1:
嘗試呼叫 static void say_hello()
main.c
#include <stdlib.h>
#include "hello.h"
int main(){
  say_hello();
return 0;
}


hello.h
#ifndef HELLO_H
#define HELLO_H
static void say_hello();
void hello();

#endif

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

#include "hello.h"
static void say_hello()
{
  printf("hello\n");
}

void hello()
{
 say_hello();

}


Makefile
#
# make file
# JoeChang
PROJECT = sample
LIBS    = 
CFLAGS  = -Wall -O2  -pthread


CC      := g++
CFILES  := $(wildcard *.c)
OBJS    = $(CFILES:.c=.o) 


.PHONY: all clean
all: clean $(PROJECT) 
@echo -e "Done,...but maybe need make clean"
-rm *.o  

$(PROJECT): $(OBJS) 
$(CC) $(CFLAGS) $(LIBS)  $(OBJS) -o  $@

$(OBJS): $(CFILES)
$(CC) $(CFLAGS) $(LIBS)  -c $(CFILES)


clean: 
@echo clean All done
-rm *.o

-rm ${PROJECT}

Result: 
    compiler error 無法呼叫static
    undefined reference to `say_hello()'
  




測試2:
main.c
#include <stdlib.h>
#include "hello.h"
int main(){
     hello();
return 0;
}



hello.h
#ifndef HELLO_H
#define HELLO_H
static void say_hello();
void hello();
#endif

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

#include "hello.h"
static void say_hello()
{
  printf("hello\n");
}

void hello()
{
 say_hello();

}


Makefile
#
# make file
# JoeChang
PROJECT = sample
LIBS    = 
CFLAGS  = -Wall -O2  -pthread


CC      := g++
CFILES  := $(wildcard *.c)
OBJS    = $(CFILES:.c=.o) 


.PHONY: all clean
all: clean $(PROJECT) 
@echo -e "Done,...but maybe need make clean"
-rm *.o  

$(PROJECT): $(OBJS) 
$(CC) $(CFLAGS) $(LIBS)  $(OBJS) -o  $@

$(OBJS): $(CFILES)
$(CC) $(CFLAGS) $(LIBS)  -c $(CFILES)


clean: 
@echo clean All done
-rm *.o

-rm ${PROJECT}

Result:
  compiler warning
  declared 'static' but never defined [-Wunused-function]


  得知: static void say_hello()不該寫再Header file

測試3:
static void say_hello()從head file移除
main.c
#include <stdlib.h>
#include "hello.h"
int main(){
     hello();
return 0;
}


hello.h
#ifndef HELLO_H
#define HELLO_H

void hello();

#endif

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

#include "hello.h"
static void say_hello()
{
  printf("hello\n");
}

void hello()
{
 say_hello();

}


Makefile
#
# make file
# JoeChang
PROJECT = sample
LIBS    = 
CFLAGS  = -Wall -O2  -pthread


CC      := g++
CFILES  := $(wildcard *.c)
OBJS    = $(CFILES:.c=.o) 


.PHONY: all clean
all: clean $(PROJECT) 
@echo -e "Done,...but maybe need make clean"
-rm *.o  

$(PROJECT): $(OBJS) 
$(CC) $(CFLAGS) $(LIBS)  $(OBJS) -o  $@

$(OBJS): $(CFILES)
$(CC) $(CFLAGS) $(LIBS)  -c $(CFILES)


clean: 
@echo clean All done
-rm *.o

-rm ${PROJECT}

Result:
  compiler pass,
  test pass.

  






測試4:   static inline
static void say_hello()全部放header file

main.c
#include <stdlib.h>
#include "hello.h"
int main(){
     hello();
return 0;
}


hello.h
#ifndef HELLO_H
#define HELLO_H


static inline void say_hello()
{
  printf("hello\n");
}

void hello();

#endif

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

#include "hello.h"

void hello()
{
 say_hello();

}


Makefile
#
# make file
# JoeChang
PROJECT = sample
LIBS    = 
CFLAGS  = -Wall -O2  -pthread


CC      := g++
CFILES  := $(wildcard *.c)
OBJS    = $(CFILES:.c=.o) 


.PHONY: all clean
all: clean $(PROJECT) 
@echo -e "Done,...but maybe need make clean"
-rm *.o  

$(PROJECT): $(OBJS) 
$(CC) $(CFLAGS) $(LIBS)  $(OBJS) -o  $@

$(OBJS): $(CFILES)
$(CC) $(CFLAGS) $(LIBS)  -c $(CFILES)


clean: 
@echo clean All done
-rm *.o

-rm ${PROJECT}

Result:
  compiler pass,
  test pass.




沒有留言: