簡體   English   中英

Makefile 忽略包含保護

[英]Makefile ignores include-guard

我正在學習makefile,但我對最簡單的問題失去了理智:

我的兩個.h 文件本身包含一個 3rd.h 文件,全部由我編寫。 如果我嘗試最簡單的編譯命令,例如“g++ main.cpp <my.cpp files> -o main -l”,我會收到多個定義警告。 我知道編譯器基本上看到了 .h 文件 3 次,但是包含保護是什么? 我該如何解決這個問題? 預處理器命令是否僅在源文件的上下文中工作而不是鏈接命令?

base.h 文件:

#ifndef HAT_H
#define HAT_H

//includes of standard libs
//enums
//prototypes
//base classes

#endif

引用.h 文件:

#ifndef HAT_AUDIO_H
#define HAT_AUDIO_H

#include "HAT.h"

//classes based on contents of HAT.h

#endif

其他引用.h 文件:

#ifndef HAT_TEMPERATURE_H
#define HAT_TEMPERATURE_H

#include "HAT.h"

//classes based on contents of HAT.h

#endif

makefile(簡化為絕對准系統):

#target: dependencies
#   action

# -o for executable
# -c for object file

all: main

main: main.cpp
    g++ main.cpp HAT.cpp HAT_Audio.cpp HAT_Temperature.cpp wav.cpp -o main --std=c++14 -I/usr/local/include -lTgBot -lboost_system -lssl -lcrypto -lpthread -lwiringPi -lasound -lfftw3 -lm

# main.o: main.cpp
#   g++ -c main.cpp

# HAT.o: HAT.cpp
#   g++ -c HAT.cpp

# HAT_Temperature.o: HAT_Temperature.cpp HAT_Temperature.h HAT.h
#   g++ -c HAT_Temperature.cpp

# HAT_Audio.o: HAT_Audio.cpp HAT_Audio.h HAT.h
#   g++ -c HAT_Audio.cpp

# wav.o: wav.cpp wav.h
#   g++ -c wav.cpp

clean:
    rm -f *.o main  

錯誤:

g++ main.cpp HAT.cpp HAT_Audio.cpp HAT_Temperature.cpp wav.cpp -o main --std=c++14 -I/usr/local/include -lTgBot -lboost_system -lssl -lcrypto -lpthread -lwiringPi -lasound -lfftw3 -lm
/usr/bin/ld: /tmp/ccDpJGVY.o:(.bss+0x0): multiple definition of `t_flag'; /tmp/ccZyJ20R.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccDpJGVY.o:(.bss+0x4): multiple definition of `hat_type'; /tmp/ccZyJ20R.o:(.bss+0x4): first defined here
/usr/bin/ld: /tmp/ccpJsQoc.o:(.bss+0x0): multiple definition of `t_flag'; /tmp/ccZyJ20R.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccpJsQoc.o:(.bss+0x4): multiple definition of `hat_type'; /tmp/ccZyJ20R.o:(.bss+0x4): first defined here
/usr/bin/ld: /tmp/ccqbbeqi.o:(.bss+0x0): multiple definition of `t_flag'; /tmp/ccZyJ20R.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccqbbeqi.o:(.bss+0x4): multiple definition of `hat_type'; /tmp/ccZyJ20R.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status

我正在學習makefile,但我對最簡單的問題失去了理智:

我的兩個.h 文件本身包含一個 3rd.h 文件,全部由我編寫。 如果我嘗試最簡單的編譯命令,例如“g++ main.cpp <my.cpp files> -o main -l”,我會收到多個定義警告。 我知道編譯器基本上看到了 .h 文件 3 次,但是包含保護是什么? 我該如何解決這個問題? 預處理器命令是否僅在源文件的上下文中工作而不是鏈接命令?

base.h 文件:

#ifndef HAT_H
#define HAT_H

//includes of standard libs
//enums
//prototypes
//base classes

#endif

引用.h 文件:

#ifndef HAT_AUDIO_H
#define HAT_AUDIO_H

#include "HAT.h"

//classes based on contents of HAT.h

#endif

其他引用.h 文件:

#ifndef HAT_TEMPERATURE_H
#define HAT_TEMPERATURE_H

#include "HAT.h"

//classes based on contents of HAT.h

#endif

makefile(簡化為絕對准系統):

#target: dependencies
#   action

# -o for executable
# -c for object file

all: main

main: main.cpp
    g++ main.cpp HAT.cpp HAT_Audio.cpp HAT_Temperature.cpp wav.cpp -o main --std=c++14 -I/usr/local/include -lTgBot -lboost_system -lssl -lcrypto -lpthread -lwiringPi -lasound -lfftw3 -lm

# main.o: main.cpp
#   g++ -c main.cpp

# HAT.o: HAT.cpp
#   g++ -c HAT.cpp

# HAT_Temperature.o: HAT_Temperature.cpp HAT_Temperature.h HAT.h
#   g++ -c HAT_Temperature.cpp

# HAT_Audio.o: HAT_Audio.cpp HAT_Audio.h HAT.h
#   g++ -c HAT_Audio.cpp

# wav.o: wav.cpp wav.h
#   g++ -c wav.cpp

clean:
    rm -f *.o main  

錯誤:

g++ main.cpp HAT.cpp HAT_Audio.cpp HAT_Temperature.cpp wav.cpp -o main --std=c++14 -I/usr/local/include -lTgBot -lboost_system -lssl -lcrypto -lpthread -lwiringPi -lasound -lfftw3 -lm
/usr/bin/ld: /tmp/ccDpJGVY.o:(.bss+0x0): multiple definition of `t_flag'; /tmp/ccZyJ20R.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccDpJGVY.o:(.bss+0x4): multiple definition of `hat_type'; /tmp/ccZyJ20R.o:(.bss+0x4): first defined here
/usr/bin/ld: /tmp/ccpJsQoc.o:(.bss+0x0): multiple definition of `t_flag'; /tmp/ccZyJ20R.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccpJsQoc.o:(.bss+0x4): multiple definition of `hat_type'; /tmp/ccZyJ20R.o:(.bss+0x4): first defined here
/usr/bin/ld: /tmp/ccqbbeqi.o:(.bss+0x0): multiple definition of `t_flag'; /tmp/ccZyJ20R.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccqbbeqi.o:(.bss+0x4): multiple definition of `hat_type'; /tmp/ccZyJ20R.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM