g++ - C++ linking issue: multiple definition -
unfortunately cannot post of source code here. can describe structure though. header files have #ifndef/#define/#endif guards. structure follows:
- node.h - included tree.h
- tree.h - included tree.cpp , main.cpp
- tree.cpp
- main.cpp
in node.h in global namespace, declare following free standing function:
bool char_sort_func(path first, path second) { return (first->label() < second->label()); }
(note: shown bellow, path shared_ptr) when try build, multiple definition error saying function present in both tree.o , main.o:
> make g++ -c -g -wall main.cpp -i /usr/include g++ -c -g -wall tree.cpp -i /usr/include g++ -wall -o tool tree.o main.o -l /usr/lib -l boost_program_options main.o: in function `char_sort_func(boost::shared_ptr<edge>, boost::shared_ptr<edge>)': node.h:70: multiple definition of `char_sort_func(boost::shared_ptr<edge>, boost::shared_ptr<edge>)' tree.o:node.h:70: first defined here collect2: ld returned 1 exit status make: *** [all] error 1
i tried searching of source files see if true, however, don't see in wrong places:
> grep char_sort_func * binary file main.o matches node.h:bool char_sort_func(path first, path second) node.h: std::sort(edges_.begin(), edges_.end(), char_sort_func); binary file trie.o matches
sure enough though, in binary files. how can restructure code prevent linking issue?
this happen if declare normal functions in .h
file, because generated in every file #include
s it. perhaps meant declare inline
or static
?
Comments
Post a Comment