程序员的自我修养:链接、装载与库(一本书,估计没时间看。。。哭。。。)

g++ -std=c++11 -I./include -I./include/asio/include main.cpp -o aaaa -L./lib -lhttp_server -lpthread

最近在写http协议栈,给其他部门的服务作为各个服务的monitor使用。涉及到自己的代码与其他人的代码整合,编译的问题,特记录一些过程。

 

待解决的问题:

1.-lpthread在cmakelists里面不好使,target_link_libraries(monitor-agent
${CMAKE_DL_LIBS}
${CMAKE_THREAD_LIBS_INIT}
) 要用${CMAKE_THREAD_LIBS_INIT}才行,为啥??

2.cmakelist编译动态库和静态库的关键字。

3.编译静态库不是使用g++...使用的事ar,是一个将若干.o打包的过程。。。

 

MakeFile的学习和书写

https://www.cnblogs.com/lidabo/p/4521117.html

https://blog.csdn.net/mgsweet/article/details/80084873

# ifndef DDK_HOME
# $(error "Can not find DDK_HOME env, please set it in environment!.")
# endif

# ifeq ($(mode),)
# mode=AtlasDK
# endif

# ifeq ($(mode), AtlasDK)
# CC := aarch64-linux-gnu-g++

# LNK_FLAGS := \
#     -L$(HOME)/ascend_ddk/device/lib -L$(DDK_HOME)/device/lib/ \
#     -shared
# else ifeq ($(mode), ASIC)
# CC := $(DDK_HOME)/toolchains/aarch64-linux-gcc6.3/bin/aarch64-linux-gnu-g++
# LNK_FLAGS := \
#     -L$(HOME)/ascend_ddk/device/lib -L$(DDK_HOME)/device/lib/ \
#     -shared
# else
# $(error "Unsupported mode: "$(mode)", please input: AtlasDK or ASIC.")
# endif


CC := g++
FLAGS := -I./include/asio/include -I./include -g -std=c++11 -fPIC -DCPU_ONLY -DASIO_STANDALONE
# FLAGS := -Iinclude/asio/include -g -std=c++11 -fPIC -DCPU_ONLY -DASIO_STANDALONE
#INC_DIR := include
SRC_DIR := ./src/http
#HTTP_DIR := http
BUILD_DIR := ./src/build
BIN_DIR := ./lib
#INCLUDE := -I./$(INC_DIR)
DIRS := $(shell find $(SRC_DIR) -maxdepth 3 -type d)

SOURCES  = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))  
OBJECTS  = $(addprefix $(BUILD_DIR)/,$(patsubst %.cpp,%.o,$(notdir $(SOURCES)))) 

$(BIN_DIR)/libhttp_server.so: $(OBJECTS)
    @mkdir -p $(BIN_DIR)
    $(CC) $(FLAGS) $(INCLUDE) -shared -fPIC $^ -o $@

# $(BUILD_DIR)/main.o: ../main.cpp
#     @mkdir -p $(BUILD_DIR)
#     #$(CC) $(FLAGS) $(INCLUDE) -c -o $@ $(SRC_DIR)/main.cpp
#     $(CC) $(FLAGS) -c -o $@ ../main.cpp

$(BUILD_DIR)/server.o: $(SRC_DIR)/server.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<
    
$(BUILD_DIR)/connection.o: $(SRC_DIR)/connection/connection.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

$(BUILD_DIR)/connection_manager.o: $(SRC_DIR)/connection/connection_manager.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

$(BUILD_DIR)/request_handler.o: $(SRC_DIR)/handles/request_handler.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

$(BUILD_DIR)/request_parser.o: $(SRC_DIR)/handles/request_parser.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

$(BUILD_DIR)/mime_types.o: $(SRC_DIR)/message/mime_types.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

$(BUILD_DIR)/request.o: $(SRC_DIR)/message/request.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

$(BUILD_DIR)/response.o: $(SRC_DIR)/message/response.cpp
    @mkdir -p $(BUILD_DIR)
    $(CC) $(FLAGS) -c -o $@ $<

clean:  
    @rm -rf $(BUILD_DIR)
    @rm -f $(BIN_DIR)/libhttp_server.so

 

(编译)使用动态库:参考b站linux全栈视频讲解

g++ -std=c++11 -I./include -I./include/asio/include main.cpp -o aaaa -L./lib -lhttp_server -lpthread

 

posted @ 2021-02-04 11:22  胖白白  阅读(187)  评论(0编辑  收藏  举报