编译简单的 hello world 程序时错误泛滥

Error-flood when compiling simple hello world program

我正在尝试编译一个简单的 hello world 程序:

main.cpp

#include <iostream>
#include <test/t.hpp>

int main( int argc , char** args )
{
    std::cout << test() << std::endl;
    int x;
    std::cin >> x;
    return 0;
}

test/t.hpp

#ifndef TEST_T_HPP_INCLUDED
#define TEST_T_HPP_INCLUDED

int test();

#endif // TEST_T_HPP_INCLUDED

test/t.cpp

#include <test/t.hpp>

int test()
{
    return 42;
}

使用此生成文件:

executable_name = testapp
cpp_compiler = g++
cpp_compiler_flags += -std=c++14
cpp_compiler_flags += -g
cpp_linker_flags += -g
linked_libraries +=
included_directories += -I./src/
source_dir = src
source_files += main.cpp
source_files += test/t.cpp
object_dir = obj
object_files = $(addprefix $(object_dir)/,$(source_files:=.o))
directories = $(sort $(foreach i,$(object_files),$(dir $i)))
source_files := $(addprefix $(source_dir)/,$(source_files))

all: make_directories tool

tool: $(object_files)
    $(cpp_compiler) $(cpp_linker_flags) -o $(executable_name) $(object_files) $(linked_libraries)

$(object_files): $(source_files)
    $(cpp_compiler) $^ $(cpp_compiler_flags) $(included_directories) -o $@

make_directories:
    @sh -c \
        'for d in $(directories); do \
            if [ ! -d $$d ]; \
                then echo mkdir -p $$d; mkdir -p $$d; \
            fi \
        done'

通过使用 CodeBlocks 13.12(和 mingw32-make)附带的 TDM-GCC-4.8.1 工具

但是我遇到了 10000 多个这样的错误:

obj/src/main.cpp.o:crtend.c:(.text+0x26a80): first defined here
obj/src/test/t.cpp.o:crtend.c:(.text+0x26880): multiple definition of `std::__us
e_cache<std::__numpunct_cache<char> >::operator()(std::locale const&) const'
obj/src/main.cpp.o:crtend.c:(.text+0x26880): first defined here
obj/src/test/t.cpp.o:crtend.c:(.text+0x350d0): multiple definition of `std::ostr
eambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambu
f_iterator<char, std::char_traits<char> > >::_M_insert_float<double>(std::ostrea
mbuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, char, double
) const'
obj/src/main.cpp.o:crtend.c:(.text+0x350d0): first defined here
obj/src/test/t.cpp.o:crtend.c:(.text+0x35ee0): multiple definition of `std::num_
put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std:
:ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, doubl
e) const'
obj/src/main.cpp.o:crtend.c:(.text+0x35ee0): first defined here

我猜这些是链接器错误,因为我可以瞬间看到要编译的源文件。

我错过了什么?


编辑

这条规则使它起作用:

$(object_files): $(object_dir)/%.o : $(source_dir)/%
    $(cpp_compiler) -c $^ $(cpp_compiler_flags) $(included_directories) -o $@

问题出在这里:

$(object_files): $(source_files)
    $(cpp_compiler) $^ $(cpp_compiler_flags) $(included_directories) -o $@

这个秘诀使得所有个源文件成为每个目标文件的先决条件。因此,当 Make 尝试构建 main.o 时,它会执行

g++ ... main.cpp test/t.cpp -o main.o

一切进入main.o一切进入t.o,所以当linker 尝试 link 将目标文件放在一起,它到处都是多个定义。

尝试静态模式规则:

$(object_files): $(object_dir)/%.o: %.cpp
    $(cpp_compiler) $< $(cpp_compiler_flags) $(included_directories) -o $@

编辑:

P.S。你可能需要帮助 Make 找到 t.cpp:

vpath %.cpp test