问题描述
我正在尝试编译一个简单的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
$ b b
#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;
}
使用此makefile:
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:$(object_files)
$(cpp_compiler)$(cpp_linker_flags)-o $(可执行文件名)$(object_files)$(linked_libraries)
$(object_files):$ )
$(cpp_compiler)$ ^ $(cpp_compiler_flags)$(included_directories)-o $ @
make_directories:
@sh -c \
'for d在$(目录); do \
if [! -d $$ d]; \
then echo mkdir -p $$ d; mkdir -p $$ d; \
fi \
done \\
使用TDM-GCC -4.8.1工具来与CodeBlocks 13.12(和mingw32-make)
但我有超过10000错误这样:
obj / src / main.cpp.o:crtend.c :(。text + 0x26a80):首先在这里定义
obj / src / test / t.cpp .o:crtend.c :(。text + 0x26880):`std :: __ us的多重定义
e_cache< std :: __ numpunct_cache< char> > :: operator()(std :: locale const&)const'
obj / src / main.cpp.o:crtend.c :(。text + 0x26880):首先定义这里
obj /src/test/t.cpp.o:crtend.c:(.text+0x350d0):`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):首先在这里定义
obj / src / test / t.cpp.o:crtend.c :(。 + 0x35ee0):多重定义`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):首先在这里定义
我想这些是链接器错误,我可以看到一个Splitsecond的源文件来编译。
我错过了什么?
EDIT
此规则使其正常工作:
$(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
/ em>进入到
,因此,当
尝试一个静态模式规则:
$(object_files):$(object_dir)/%。o:%.cpp
$(cpp_compiler)$& $(cpp_compiler_flags)$(included_directories)-o $ @
EDIT:
PS您可能必须帮助找到 t.cpp
:
vpath% .cpp test
I'm trying to compile a simple hello world program:
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;
}
With this makefile:
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'
By using the TDM-GCC-4.8.1 tools coming with CodeBlocks 13.12 (and mingw32-make)
But I get over 10000 errors like this:
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
I guess those are linker errors, as I can see for a splitsecond the source files to get compiled.
What did I miss here?
EDIT
this rule made it work:
$(object_files): $(object_dir)/%.o : $(source_dir)/%
$(cpp_compiler) -c $^ $(cpp_compiler_flags) $(included_directories) -o $@
The problem is here:
$(object_files): $(source_files)
$(cpp_compiler) $^ $(cpp_compiler_flags) $(included_directories) -o $@
This recipe makes all source files prerequisites of every object file. So when Make tries to build main.o
, it executes
g++ ... main.cpp test/t.cpp -o main.o
Everything goes into main.o
, and everything goes into t.o
, so when the linker tries to link the object files together, it hits multiple definitions everywhere.
Try a static pattern rule:
$(object_files): $(object_dir)/%.o: %.cpp
$(cpp_compiler) $< $(cpp_compiler_flags) $(included_directories) -o $@
EDIT:
P.S. you might have to help Make find t.cpp
:
vpath %.cpp test
这篇关于编译简单hello world程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!