Registered Member
|
I have some general purpose classes and functions that are duplicated (copy/paste) in two different projects. I moved this redundant code into a single header and source file. Both files were put into a different directory like this:
/home/carol/project_1 <-- my first project /home/carol/project_2 <-- my second project /home/carol/my_toolkit <-- my_tools.h, my_tools.cpp Then I added the following to CMakeLists.txt: include_directories(../my_toolkit/) The application compiles fine but fails during the link process. I am assuming that the object files for my_tools.h/cpp are not found. Can someone tell me what to do next? Thank you P.S. It may have something to do with the following. I believe that I need to figure out how to add my_tools.h/cpp to add_executable: file(GLOB src "*.h" "*.cpp") add_executable(myapp ${src}) |
Registered Member
|
My link problem was resolved by altering the following line from this:
file(GLOB src "*.h" "*.cpp") to this: file(GLOB src "*.cpp" "../my_toolkit/*.cpp") Building just fine after the modification. |
Global Moderator
|
Hi,
glad you found a solution. Just some hints: - I don't recommend using glob, it will not rebuild things if you add a file because cmake doesn't know when to re-run the glob. Instead, list all the source files by hand. - Instead of building the cpp files for each project, you can build them into a shared or static library, and link against that library. That will reduce build time significantly. - Instead of including directories from elsewhere, you typically install this shared or static library and the headers together with a CMake config file (mytoolkit-config.cmake) and then use find_package(MyToolkit) in the depending project. Greetings, Sven
I'm working on the KDevelop IDE.
|
Registered Member
|
Hello Sven,
Thank you for your help and for working on KDevelop! I am fairly new to Linux world but I love it more and more!
Initially, I was manually listing source files, but found it easier to use glob and make a fake change to CMakeLists.txt (e.g. add a blank line) each time I add new source file . I find this to be less work than manually adding the new file name. Of course, when you rename a file, you must touch CMakeLists.txt again. Another benefit to glob that I just learned, was that if you add your *.h files (to be globbed), any time you alter one of your specific headers, your corresponding object file gets rebuilt automatically! This provides you with automatic behavior where a change to a header or a source file gets the respective object file(s) recompiled and rebuilt.
I want to eventually build libraries, especially dynamic (shared) libraries and follow your find_package advise. When moving from Windows to Linux, the concepts are surprisingly similar but the details are totally different; the advise you provided me with is very helpful and reassuring. Thank you! |
Global Moderator
|
The problem is that eventually somebody else wants to work on your project, and that person will not know that she needs to do that. That's why using glob is generally not advised for this purpose. The second benefit actually doesn't exist -- cmake does this by itself, by asking the compiler for inter-file dependencies and instructing the build executor to do the necessary rebuilds. Adding the .h files to your target gains you nothing at all. Best of luck, Sven
I'm working on the KDevelop IDE.
|
Registered users: Bing [Bot], daret, Google [Bot], Sogou [Bot]