Adding include files information to library in CMake - include

I'd like to use imported and internal libraries in my CMake projects so that the projects don't need to know the details of the library. (By "Internal library" I mean other CMake library targets, not sure about the correct term...)
The information should contain:
library locations for each configuration type (including dll's and .pdb files)
library include files folder
In some project I'd like to write something like:
SET(TARGET_DEPENDS ext_lib1 ext_lib2 internal_lib1)
and let the build system take care of all the include folder and configuration stuff.
What is the best way to achieve this?
AFAIK the add_library(... IMPORTED) supports pretty much everything else, but the include folder information.

When building your "internal" library, you could also prepare a YourInternalLibraryConfig.cmake file which prepares all those things a user would need to "import" the library.
This YourInternalLibraryConfig.cmake needs to be created when building the internal library or when installing it.
In this YourInternalLibraryConfig.cmake, it should call add_library( YourInternalLibrary IMPORTED ) and make sure all the properties have been correctly prepared. E.g. it can prepare
a YourInternalLibrary_INCLUDE_DIRS variable with the necessary include-dirs.
a YourInternalLibrary_DEFINES with the required compiler-defines
a YourInternalLibrary_LIBRARIES summarizing all the libraries provided
That way a user (e.g. the main app ) of your internal library can simply call:
find_package( YourInternalLibrary )
include_directories( ${YourInternalLibrary_INCLUDE_DIRS} )
add_definitions( ${YourInternalLibrary_DEFINES} )
target_link_library( main ${YourInternalLibrary_LIBRARIES} )
For more complex projects you could choose to generate a separate UseYourInternalLibrary.cmake which does these three steps for you, optionally with switches enabling/disabling certain parts of your internal.
For some good examples take a look at the sources for Vtk or OpenCV

Related

Merging multiple dynamic libraries into one using Cmake

I got a project that uses Cmake and has a configuration to create a shared library. This project has some external dependencies (like zlib, etc). Generating shared library and linking against it would require not only providing dll generated by the project, but also dlls for all dependancies (since the library itself is linked against other libraries).
I'm wondering if it's possible to create a single dll of that project, that would have statically linked all dependencies into it. I tried creating an OBJECT library and using it to create a SHARED library with Cmake, but had no luck with this approach. Is this can be achieved?
Unfortunately static linking of the whole library is not an option due to licensing issues and I'd like to avoid attaching 10 dlls to the application package. I need that only for Windows.

find_package for both debug and release with Visual Studio

I'm tearing my hair out about how to include thrid party libraries in my cmake project. Currently I build Poco and a bunch of others that all generate their respective Config.cmake which I use with find_package. I have a wrapping build script that builds all of my dependencies and package them separately for debug and release (I don't want to tweak their cmake-scripts unless I really really really need to because maintanance).
I thought I could just do:
find_package(Foo
HINTS "${CMAKE_SOURCE_DIR}/some/path/debug/libFoo/lib/cmake"
REQUIRED
)
get_target_property(LIB_FOO_DEBUG lib_foo LOCATION)
find_package(Foo
HINTS "${CMAKE_SOURCE_DIR}/some/path/release/libFoo/lib/cmake"
REQUIRED
)
get_target_property(LIB_FOO_RELEASE lib_foo LOCATION)
set(LIB_FOO_LIBRARIES optimized "${LIB_FOO_RELEASE}" debug "${LIB_FOO_DEBUG}")
message("LIB_FOO_LIBRARIES: \"${LIB_FOO_LIBRARIES}\"")
This yeilds: LIB_FOO_LIBRARIES: "optimized;C:/path/to/proj/some/path/debug/libFoo/lib/foo.lib;debug;C:/path/to/proj/some/path/debug/libFoo/lib/foo.lib"
It seems like the first call to find_package for target Foo is cached, whis I don't really want.
Am I going about this the wrong way? How do I properly work with third party libraries with the Visual Studio generator?
Any pointers are greatly appreciated.
the first call to find_package for target Foo is cached
Yes. So you cannot issue find_package twice and get different results (unless the first call failed).
It is third-party package who is responsible for multiconfig-usage, that is it should have properly written *Config.cmake/Find*.cmake file. (E.g., FindBoost.cmake support multi-config usage).
Otherwise, you should do some tricks for use package in multiconfig manner.
E.g., if you guess that only difference between configurations is debug/release substrings in paths, you can call find_package() for debug installation, then use string(REPLACE) for get release-specific paths:
find_package(Foo
HINTS "${CMAKE_SOURCE_DIR}/some/path/debug/libFoo/lib/cmake"
REQUIRED
)
get_target_property(LIB_FOO_DEBUG lib_foo LOCATION)
string(REPLACE debug release LIB_FOO_RELEASE ${LIB_FOO_DEBUG})
# Use generator expressions, so variable can be used not only by target_link_libraries.
set(LIB_FOO_LIBRARIES
"$<$<NOT:$<CONFIG:DEBUG>>:${LIB_FOO_RELEASE}>"
"$<$<CONFIG:DEBUG>:${LIB_FOO_DEBUG}>"
)

Why are Frameworks Linked As Binaries In Xcode?

You call Library code and Framework code calls your code.
I've been doing a number of xcode projects which required adding external frameworks and libraries.
In the installation of these there was always one common occurance, they would always ask me to go to the Build Phases tab and "link the binary with libraries"
My question is why do both frameworks and libraries get linked this way? I (think) I understand why a library does. For libraries they can't execute themselves so you are pre-compiling them and making their functions available to your classes.
For the framework I am much less clear whats happening here. If the framework calls my code why is this being pre-compiled as a binary? If it's executing my code at runtime should it just be compiled along with the build?
A Framework is a way to bundle a static/dynamic library and it's headers (.h) together. There is also a concept of "versioning" but thats not really used anymore.
If you look into a Framework (its just a directory ), you will see the library (without the .a and "lib" prefix , headers, and a .plist.

How to organize C source file previously compiled by GCC Make and build them into an Xcode bundle? I have a Duplicate Symbol _main Error

How to build a .bundle from source code?
This might sound like a simple problem but it has been hurdling me for a week...
Here is my problem:
I have a bunch of .c and .h files that are organized in a folder and its sub folders. The source code was written and compiled with gcc make and tested by many other make tools. The source code has some utilities and command line tools and it has more code that serve as library for those utilities and tools. It is the files that serve as libraries that I want to reuse. (By library I don't mean static library or something, I just mean that some .c and .h files in certain subfolders provide functions that can be called by some other .c files. I want to be able to call those functions, too)
Yet my problem is more complex than that: I need to build those .c and .h into a bundle to reuse it. I am not writing my application in C; I am developing in Unity and Unity can only take in .bundle files on Mac OS.
Here is my goal:
Organize the source code folder in a proper way so that I can build them into a bundle in Xcode 4.
Here is where I got stuck:
When building the project I got the following error:
Duplicate symbol _main in
/Users/zeningqu/Library/Developer/Xcode/DerivedData/ccn-cfygrtkrshubpofnfxalwimtyniq/Build/Intermediates/ccn.build/Debug/ccn.build/Objects-normal/i386/ccndsmoketest.o
and
/Users/zeningqu/Library/Developer/Xcode/DerivedData/ccn-cfygrtkrshubpofnfxalwimtyniq/Build/Intermediates/ccn.build/Debug/ccn.build/Objects-normal/i386/ccnd_main.o
for architecture i386
I can relate to this error because I can find lots of main entries in the source code. Most of them are test utilities.
Here is what I tried:
I tried removing all those utility .c files but with no luck. The error is still there. I delete and delete until some files cannot find the definition of the function they are calling. So I had to stop there.
Though I wasn't able to build a bundle I was able to build a C/C++ static library (with an .a extension). After I got the .a file I tried to put it into another Xcode project and tried to build it into a bundle. I could build a bundle in that way, but then I had problem accessing the content of the bundle. How do I call functions defined in a .a static library if that library is hidden in a bundle? I read about Apple's documentation which says:
Note: Some Xcode targets (such as shell tools and static libraries) do
not result in the creation of a bundle or package. This is normal and
there is no need to create bundles specifically for these target
types. The resulting binaries generated for those targets are intended
to be used as is.
(quoted from: https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/AboutBundles/AboutBundles.html#//apple_ref/doc/uid/10000123i-CH100-SW1)
Here is what I thought about:
I thought about replacing all main with something like main_sth. But the source code was not written by me so I didn't want to modify it. (It just doesn't feel like a proper way of doing things to me...)
I learnt that Xcode has gcc compiler built in. So I guess if gcc can make it, so can Xcode? It's just a wild guess - I am not familiar with Xcode and gcc.
Here is a summary of my questions:
Is there a way to properly organize a pile of code previously compiled and made by gcc make so that they can be built into an Xcode bundle?
Is it meaningful to put a .a library in an Xcode project and build it into a bundle? If it is meaningful, how do I call functions defined in .a after it is built into a bundle?
Is it proper to just replace all main() entries with something else?
Alright I think I have figured out at least one solution to the problem.
The duplicate main error was caused by a bunch of main entries in my source code. When the code was compiled by gcc make, I guess the author defined a sort of compilation order so that duplicate mains won't be an issue. (If you know how to do this, please let me know. I barely know make tools.) But when I just add the entire source code folder into my Xcode project, of course Xcode would complain during linking...
As I was unwilling to modify the source code (because the source code library is not developed by me), I decided to use another strategy to walk around this problem.
If your duplicate main error was reported from your own code, you can stop reading here. But if you are like me, with a bunch of gcc compiled source code and badly need a bundle yet don't know what to do, I may be able to help.
Okay here is what I did:
I set up an empty workspace.
I built a C/C++ static library project.
Import my entire source code folder into the static library project.
Set some header search path for the static library project.
Build the static library project. (Now I have a .a library which I could link against)
I set up another project, with a bundle target.
At the bundle project -> Build Phases -> Link Binary with Libraries, add the .a library that I just built.
At the bundle project -> edit scheme -> Build, add the static library project to the scheme and move it up the list so that it is built prior to my bundle project.
Then add .h files of my library project to my bundle project as references.
After that, add a .c file in my bundle project that basically functions as a wrapper. I picked a function that I want to call in Unity, wrote a wrapper function in the new .c file, and was able to build the bundle.
After several trial and error, I was able to import the bundle into Unity and was able to call the test function from Unity.
I was really excited about this! Though it's not completed yet I think this gives me hope and I am confident I can use the source code now! And the best thing about this solution is that I don't have to modify the library code developed by others. Whenever they update their code, I just update my .a library and that's it!
Though I have listed 11 steps I still feel that there are lots of details that I missed. So here are my references:
I followed this tutorial to build my source code into a static library: http://www.ccnx.org/?post_type=incsub_wiki&p=1315
I followed this blog to link static library against my bundle code and twist build phases and search headers: http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/
I followed this doc to import my bundle to Unity3D Pro as a plugin: http://unity3d.com/support/documentation/Manual/Plugins.html
I strongly recommend the second reference because that's what solved my problem!
Though the problem is almost solved there are still a few things that I haven't figured out:
I don't know if a wrapper function is at all necessary. I will try this out tomorrow and come back to update.
-- I am coming back to update: the wrapper function is NOT necessary. Just make sure you have all the headers in your bundle project and you will be able to use all the data structures and call functions defined in your headers.
I haven't used NSBundle class though I read a few docs about it. Previously I was thinking about using that class to access my .a library encapsulated in my bundle, but as I found the solution I wrote above, I didn't try the class out.
Lastly, if you have better solution, please don't hesitate to let me know!
I tried to follow the steps in the accepted answer, but had no luck. In the end, I realised step 10 needed to be modified slightly:
Create a dummy.c under (.bundle) project and the dummy.c can just be totally empty.
Remove the setting for the library you want to link inside Link Binary With Libraries
Instead use -Wl,-force_load,$(CONFIGURATION_BUILD_DIR)/libYourLib.a or -all_load to Other Linker Flags
PS: And also can use sub-project instead of workspace. and use Target Dependencies instead of Edit Scheme to achieve the same effect.

library examples build in eclipse

I have C project of a library (using CDT). Configurations for both static and dynamic linking for several platforms. Several examples of the library usage is also included in the project. What is the best way to build these examples with the library? If I would like to build both the library and examples (linking the library just built) in one configuration?
I suppose I have to use custom makefile. Do I have to create makefile for the whole project (several of them, one for each platform), or is there any way how to include examples makefile to the automatic one?
Each example has only one source file, so the only things I need to do in my makefile are to determine which compiler is used, add some flags and link with the library which was built (I would include the make examples command as the post-build step).
As I didn't find any solution for this, I use custom makefile for the whole build. I also found a nice advice somewhere: if you want advanced build functions, use advanced build system.

Resources