c++ - Will not link, unless inlining method -


i facing strange error @ linking time.

the headers:

global.h

#include <cmath> #include <iostream> #include <vector>  #include <blitz/tinyvec2.h> typedef blitz::tinyvector<double,3> vettore;  #include "animal.h" 

animal.h

#ifndef __animal_h #define __animal_h   //! basic data structure animal ------------------------- struct animal {    int age; public:    animal(int _age) : age(_age) {} };   //! contains info pair of animals ---------------------- struct animalpairs {   vettore distance;    animalpairs( const vettore& _distance ) : distance(_distance) {} }; typedef std::vector<animalpairs> pair_list;   //! data structure group of animals ---------------------- class animalvector { private:   std::vector<animal> animals;   pair_list pairs;  public:   animalvector( const  animalvector &other );  };  #endif 

and here *cpp files

main.cpp

#include "global.h" int main () {    std::cout<< "hello" << std::endl; } 

animal.cpp

#include "global.h"  animalvector::animalvector( const animalvector &other ) {   pairs = other.pairs; } 

to compile use g++ main.cpp animal.cpp -i/usr/include/boost -i/fs01/ma01/homes/matc/local/blitz/include

here error get:

/tmp/ccgkhwoj.o: in function `animalpairs::animalpairs(animalpairs const&)': animal.cpp:(.text._zn11animalpairsc2erks_[_zn11animalpairsc5erks_]+0x1f): undefined reference \`blitz::tinyvector<double, 3>::tinyvector(blitz::tinyvector<double, 3> const&)' collect2: error: ld returned 1 exit status 

for reasons, if set animalvector constructor inline, code work. can explain me why?

edit: here link blitz/tinyvec2.h https://github.com/syntheticpp/blitz/blob/master/blitz/tinyvec2.h

tinyvec2.cc file need include when use methods declared in tinyvec2.h

i dislike naming (an include file named .cc) , have overlooked myself before getting error got.

but if @ contents of .cc file, intended usage clear.

in similar situations (with different naming rules pairs of files) use following coding idiom:

the first include file (their .h) included other .h file needs of this. second include file never included other .h files.

when build error indicating of missing, add include of second include file .cpp file gets build error.

that plan effective keep build speeds fast in mega projects , minimize circular dependence among complicated interacting template classes. might not whoever split tinyvec2 2 include files had in mind.

bottom line: module getting link error saw needs have had .cc file included @ compile time, directly vs. indirectly you. including .cc in global.h simpler, , @ worst slow down build speed (because .cc not have circular relationships own .h files).


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -