[CMake] works when I build using XCode, but not with CMake makefile
Peter Steinbach
steinbach at scionics.de
Wed Sep 7 03:36:31 EDT 2016
Hi Mr Candy (I am still getting a heck out of cottoncandycoder, sorry :D )
1) the way you do it, is not really the way cmake should be used AFAIK.
> In my CMakeLists.txt file I included:
> set( CMAKE_CXX_FLAGS "-L/Applications/MAMP/Library/lib -lmysqlclient
> -lpthread -lz" )
> set( CMAKE_EXE_LINKER_FLAGS "-lmysqlclient -lpthread -lm -lz" )
change this to:
#I assume you have something like this somewhere
add_executable(my_exe_name SOURCES my_exe_name.???)
#here comes the "magic"
link_directories(/Applications/MAMP/Library/lib)
target_link_libraries(mysqlclient pthread m z)
In theory, if all those dependencies are available at cmake-invocation, this should emit compiler calls that produce your binary and link mysqlclient into it (by default with using RPATH, see the docs on this:
https://cmake.org/cmake/help/v3.0/prop_tgt/MACOSX_RPATH.html#prop_tgt:MACOSX_RPATH
). Depending on whether you wanna distribute your binary and cannot be sure if (at build time) pthreads etc are available, there are cmake find modules for pthreads and libz (FindThreads, FindZLIB) which you can use. libm should come with the libc of the system AFAIK.
I also just checked but up to cmake 3.5, there is no MYSQL Find module. Which is kinda sad as there is a FindPostgreSQL module. :( If there would be, you could use it in a (hopefully) platform independent way and not bother with finding the right paths to libmysqlclient.
2) The compiler flags you posted do not explain, why Xcode apparently sets the rpath inside the binary and your cmake script doesn't (haven't seen the full CMakeLists.txt of your project yet).
I hope the above gets you going.
@cmake developers: it would be nice to have more obvious pointers to cmake example projects like an example SDK or so. If there is, please let me know. I only found this:
http://www.vtk.org/Wiki/CMake/Examples#Finding_Packages
but that's tied to vtk.
Best,
peter
On 09/06/2016 08:12 PM, Cotton Candy wrote:
> Peter,
> In XCode I have this list of "settings" that includes
> "Other Linker Flags" that I have set to "-lmysqlclient -lpthread -lm -lz"
> and
> "Other C++ Flags" that I have set to "-L/Applications/MAMP/Library/lib
> -lmysqlclient -lpthread -lz"
>
> Maybe these explain why things work when I build with XCode, but not with
> CMake.
>
>
> but when I run the make it always says it is ignoring these (e.g. "warning:
> argument unused during compilation: '-L/Applications/MAMP/Library/lib'").
>
> Thanks again for you help.
> Aaron
>
>
>
>
>
> On Tue, Sep 6, 2016 at 2:20 PM, Peter Steinbach <steinbach at scionics.de>
> wrote:
>
>> Aaron,
>>
>> it's about the way that you compile your binary and link libmysqlclient
>> into it. I guess (@all: please correct me if I am wrong) as I don't know
>> how you use cmake to build your libraries/binaries, that you don't set the
>> rpath of libmysqlclient inside your binary. Doing so will ensure that the
>> absolute path of libmysqlclient is stored into your binary, so that the
>> runtime environment can pick it up and use (keeping fingers crossed that
>> the path is still valid). The alternative to doing so, is linking against
>> the static version of libmysqlclient (which comes at a cost on another
>> front as well).
>>
>> Best,
>> P
>>
>
More information about the CMake
mailing list