嘗試呼叫 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()'
#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]
測試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.
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.
將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
將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");
}
#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.
沒有留言:
張貼留言