[CMake] newbie question about inline functions

Timenkov Yuri ytimenkov at gmail.com
Wed Sep 24 12:19:29 EDT 2008


On Wed, Sep 24, 2008 at 5:11 AM, Roy Zuo <roylzuo at gmail.com> wrote:

> Thanks a lot, and list(REMOVE_ITEM ...) works well for me.
>
> Another stupid question, as I am really new to c++. There are some cpp
> files containing only inline functions and some template class
> functions. The could not be compiled correctly as well, but if I
> exclude them from compilation, errors will come up in the linking
> stage. How should I treat those files?

You should include these files into .cpp sources where these inlines are
used.
For example, I've used something like this:

file1.h:
template<typename T>
class C
{
public:
T myfunc();
};

file1_inl.h:
template<typename T>
T C::myfunc()
{
  // Implementation goes here.
}

file2.h
#include "file1.h"
// Use template class declared previously.
typedef C<int> myC;

file2.cpp:
#include "file2.h"
#include "file1_inl.h"
// Here we can call myC::myfunc(), because its implementation is accessible
from here.

I can be wrong, because I don't know how exactly you can define template
classes and functions' implementations, but generally this should work.


>
> Roy
>
> On Mon, Sep 22, 2008 at 06:00:52PM +0400, Timenkov Yuri wrote:
> >
> >     Hello eveyone,
> >
> >     I am just new to cmake and have some trouble when compiling a big
> >     project whose cpp source files are put deep inside subdirectories.
> >
> >     In my CMakeLists.txt, I use AUX_SOURCE_DIRECTORY(subdir VARX) and
> >     SET(SRC ${VAR1} ${VAR2} ...) to put all the source files in a
> >     variable, and then use ADD_EXECUTABLE(foo main.c ${SRC}) to make the
> >     executable. However, there are some cpp files containing only inline
> >     functions, and when complier tries to compile them into .o files, a
> >     lot of errors are throw out. So my question is, how to exclude those
> >     files from the compilation?
> >
> >     I know this question is naive because this is my first time working
> >     with cmake as well as C++.
> >
> > There are a lot of solutions. One (possible most correct) is to keep in
> cpp
> > files only code which should be compiled. Put other code to headers. For
> > example, we've used ".inl" or "-inl.h" files for template code which were
> > included only to .cpp files (keeping prototypes in separate header).
> >
> > Next, you can try using list(remove_item) function as work-around for
> your
> > problem.
> > For example, you know exact list of your not-to-compile files:
> > set(MY_INLINE_FILES file1.cpp file2.cpp)
> > list(remove_item SRC ${MY_INLINE_FILES})
> >
> > OR, It will be better to exclude such files from compilation, keeping
> them in
> > target's sources, so CMake will put them into corresponding VS project:
> > set_source_files_properties(${MY_INLINE_FILES} PROPERTIES
> HEADER_FILE_ONLY
> > TRUE)
> > So, generated build system will not try to compile these files. We used
> this
> > approach to include all sources into Studio projects (including
> linix-specific
> > ones).
> > Also, you can mark them:
> > source_group("Inline files" ${MY_INLINE_FILES})
> >
> >
> >
> >
> >     Kind regards,
> >
> >     Roy
> >     _______________________________________________
> >     CMake mailing list
> >     CMake at cmake.org
> >     http://www.cmake.org/mailman/listinfo/cmake
> >
> >
>
> > _______________________________________________
> > CMake mailing list
> > CMake at cmake.org
> > http://www.cmake.org/mailman/listinfo/cmake
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20080924/97ee589c/attachment.htm>


More information about the CMake mailing list