CMake generates VC solution with incorrect target link library prefix - visual-studio-2012

I'm having trouble trying to do something seemingly very simple with CMake 2.8.11.2 . I have a folder with two files:
-- CMAkeLists.txt --
add_executable(test test.c)
target_link_libraries (test somelib)
-- test.c --
// Some c code
when I create a build directory and issue cmake .., cmake runs with the following output:
C:\Users\Enis\workspace_kepler\tmp\build>cmake ..
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Enis/workspace_kepler/tmp/build
And an MSVC 2012 solution is generated inside the build folder (as I have MSVC2012 installed). Now, the problem is that when I open that solution and check the linker configuration of the test project under Properties->Configuration Properties->Linker->Input->Additional Dependencies I see that somelib is added as ;-lsomelib.lib and visual studio doesn't like that. It links successfuly only if I change that to somelib.lib manually.
What am I doing wrong? Why does CMake make such a simple mistake? What can I do to change the prefix it generates?

I faced the same issue. The root of the problem is CMAKE_LINK_LIBRARY_FLAG but I have no idea what sets it. Resetting it, demonstrated in the snippet below, should help you.
set(CMAKE_LINK_LIBRARY_FLAG "")

Related

Shared Libraries not linking together after installation with CMake

I have run into a rather strange problem with Google Tests.
In my project, I am using externalProject_add in order to download google tests and add them into my project. In my function, I believe I am asking for the project to be built, and then installed into a specific directory:
ExternalProject_Add(gTest_download
URL ${GTEST_url}
URL_HASH ${GTEST_hash}
UPDATE_COMMAND ""
BUILD_COMMAND cmake --build . --target install
CMAKE_CACHE_ARGS
-DCMAKE_C_COMPILER:PATH=${Compiler_C}
-DCMAKE_CXX_COMPILER:PATH=${Compiler_CXX}
-DBUILD_SHARED_LIBS:BOOL=ON
-DCMAKE_INSTALL_PREFIX:PATH=<BINARY_DIR>/installation
)
I can then tell the program where all the source files are living with this:
ExternalProject_Get_Property(gTest_download BINARY_DIR)
set(gTest_LIBRARY_DIR ${BINARY_DIR}/installation/lib CACHE INTERNAL "Google Test Binary Dir")
set(gTest_INCLUDE_DIR ${BINARY_DIR}/installation/include CACHE INTERNAL "Google Test Include Dir")
However, when I try to run a cmake test with protobufs I get the run time error:
./Protobuf_test: error while loading shared libraries: libgmock.so.1.11.0: cannot open shared object file: No such file or directory
Which is super odd, because I know I specifically told the program where to find the libraries in the same externalProject_add file:
set(gTest_LIBRARIES
${gTest_LIBRARY_DIR}/${prefix}gmock${suffix}
${gTest_LIBRARY_DIR}/${prefix}gmock_main${suffix}
${gTest_LIBRARY_DIR}/${prefix}gtest${suffix}
${gTest_LIBRARY_DIR}/${prefix}gtest_main${suffix}
CACHE INTERNAL "Google Test Libraries"
)
Where ${prefix} is "lib" and ${suffix} is ".lib". And I make sure to link them in my CMakeLists.txt file properly by doing target_link_libraries(Protobuf_test ${gTest_LIBRARIES} ${protobuf_LIBRARIES}) ex:
CUSTOM_PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDS ${CMAKE_CURRENT_LIST_DIR} hello.proto)
include_directories(
${protoBuf_INCLUDE_DIR}
${gTest_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR}
)
add_executable(Protobuf_test protobuf_test.cc ${PROTO_SRCS} ${PROTO_HDS})
add_dependencies(Protobuf_test
gTest_download
protoBuf_download
)
target_link_libraries(Protobuf_test
${gTest_LIBRARIES}
${protoBuf_LIBRARIES}
)
add_test(NAME testing_protobuf COMMAND Protobuf_test)
So I went into the installation folder which is located in d/linuxBuild/lib/src/gTest_download-build/installation/lib and confirmed it exists there. I then ran ldd libgmock.so and got the following output:
libgtest.so.1.11.0 => not found
Which I thought was odd as well. gtest is in the same directory! How is that possible? So I ran ldd on gmock_main:
libgmock.so.1.11.0 => not found
libgtest.so.1.11.0 => not found
So now I have two libraries that are in the same directory however they cannot be found. Confused, I decide to go to where the libraries should have originally installed to and copied over from. So two folders up: d/linuxBuild/lib/src/gTest_download-build. I then go into that folders lib folder and verify the libraries are there. I then run the same ldd command on gmock:
libgtest.so.1.11.0 => /mnt/d/linuxBuild/lib/src/gTest_download-build/lib/libgtest.so.1.11.0 (0x00007fb651729000)
I'm confused by this and again, run it on gmock_main:
libgmock.so.1.11.0 => /mnt/d/linuxBuild/lib/src/gTest_download-build/lib/libgmock.so.1.11.0 (0x00007f58b0db3000)
libgtest.so.1.11.0 => /mnt/d/linuxBuild/lib/src/gTest_download-build/lib/libgtest.so.1.11.0 (0x00007f58b0c9c000)
I am sorry for the lengthy question, but I need to know what happened here? Why is it when I install the libraries the links break from each other and they don't know their locations compared to the ones in the original installation path? Did their symbolic links break? Did I do something incorrectly in the CMake build? I'm scratching my head on this problem since I have never encountered this before. Any advice would be greatly appreciated.
Forget about externalProject_add and use FetchContent / FetchContent_MakeAvailable, especially when dealing with CMake-ready projects:
See https://cmake.org/cmake/help/latest/module/FetchContent.html for details
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
)
FetchContent_MakeAvailable(googletest)
# ...
target_link_libraries(Protobuf_test
PRIVATE
gmock_main
${protoBuf_LIBRARIES}
)
This way, you are linking against the gmock_main target, which will set up your libraries, includes, and any indirect dependencies correctly like any cmake project added via add_subdirectory() or find_package().

Is cmake work differently for Linux and WinCE platform?

We are working on the Project which contains thousands of the cmake files. Code is develop on the Linux platform. Now we are porting this project for Windows CE platform.
We are facing lots of linking error when change some of the functionality to project in Linux platform.
We are resolving this linking error by adding respective library in target_link_library of cmake file.
I am surprised how it worked for Linux? And it is failing for Windows CE.
Difference between the platform additional library :
Linux Platform
[A.Common]
[B]
[C]
[C]
[C]
[TLV]
[D]
[Boost.Filesystem]
[Boost.System]
[Boost.Thread]
[Boost.Atomic]
[Boost.DateTime]
[Boost.Chrono]
[Platform.Socket]
[D]
[C]
[C]
[Debug]
[SQLite]
[C]
[C]
[C]
[Meta]
[E]
WinCE
[A.Common]
[B]
[E]
[E]
[E]
[TLV]
[D]
[Boost.Filesystem]
[Boost.System]
[Boost.Thread]
[Boost.Atomic]
[Boost.DateTime]
[Boost.Chrono]
[Platform.Socket]
[D]
[C]
[C]
[Debug]
[SQLite]
[ZLib]
[C]
[C]
[C]
[Meta]
[E]
Any help will be appreciated.
From my experience I can confirm that moving with a CMake project to a new platform is also a check if you have setup your library dependencies correctly.
If we are talking about SHARED libraries, please first confirm that you have correctly exported your function declarations:
Creating and using shared libraries with different compilers on different operating systems
cmake link shared library on Windows
If you are using STATIC libraries, I see two possible approaches (not taking linker errors about missing symbols from system libraries into account):
You may have some if statements checking for this or that platform in your CMake code. So first check that your library dependencies are truly the same by activating GLOBAL_DEPENDS_DEBUG_MODE:
set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)
Put it in your main CMakeList.txt file and compare the output on both platforms.
The linking order is also important, not only that the library is named somewhere on the linker command line. I like to quote from the Beginner's Guide to Linkers:
Another important detail to note is the order of events; the libraries are consulted only when then the normal linking is done, and they are processed in order, left to right. This means that if an object pulled in from a library late in the link line needs a symbol from a library earlier in the link line, the linker won't automatically find it.
Use the following CMake test program to see if your linker does a multi-path scan for open symbols. I've tested it with GCC 4.8.1 (doesn't work) and Visual Studio 2013 (does work). So I don't have your environments, so please do the test yourself.
The program is deliberately setup to have a wrong library linkage order by not using normal library dependencies like target_link_libraries(lib1 lib2) (a rule of thumb is to add as dependecies all libraries you directly include a header file from), but by giving the order in a global list to main:
cmake_minimum_required(VERSION 2.8)
project(WrongLinkOrderTest CXX)
set(_idx 1)
while (_idx LESS 10)
math(EXPR _next_idx "${_idx} + 1")
file(WRITE lib${_idx}.h "int lib${_idx}_func();")
file(WRITE lib${_idx}.cc "#include \"lib${_next_idx}.h\"\nint lib${_idx}_func() { return lib${_next_idx}_func(); }")
add_library(lib${_idx} lib${_idx}.cc)
# NOTE: This would fix it
#target_link_libraries(lib${_idx} lib${_next_idx})
set(_idx "${_next_idx}")
endwhile()
file(WRITE lib${_idx}.h "int lib${_idx}_func();")
file(WRITE lib${_idx}.cc "int lib${_idx}_func() { return 0; }")
add_library(lib${_idx} lib${_idx}.cc)
file(WRITE main.cc "#include \"lib1.h\"\nint main() { return lib1_func(); }")
add_executable(main main.cc)
if (CMAKE_COMPILER_IS_GNUCXX)
#target_link_libraries(main "-Wl,--start-group")
endif()
while (_idx GREATER 0)
math(EXPR _next_idx "${_idx} - 1")
# NOTE: Here it's done wrong
target_link_libraries(main lib${_idx})
set(_idx "${_next_idx}")
endwhile()
if (CMAKE_COMPILER_IS_GNUCXX)
#target_link_libraries(main "-Wl,--end-group")
endif()
By default, it will show the following error with GCC:
liblib1.a(lib1.cc.obj):lib1.cc:(.text+0x7): undefined reference to `lib2_func()'
Uncomment the target_link_libraries(lib${_idx} lib${_next_idx}) or --start-group/--end-group lines to fix it.
Additional References
How do I list the defined make targets from the command line?
Why does the order in which libraries are linked sometimes cause errors in GCC?
GCC: what are the --start-group and --end-group command line options?
Porting code to Windows and getting "error LNK2019: unresolved external symbol ...."
Use -Wl,--start-group and -Wl,--end-group for Android linking

cmake error when compiling with arm-linux-android standalone toolchain but make can not be executed

I want to compile a native code writing by C with a arm-linux-android Compiler from android-ndk, so that that codes can be executed in a android pad.
First I write a Toolchain_armgcc_android.cmake. In this file i have put the codes
set(CMAKE_STRIP arm-linux-androideabi-strip)
set(CMAKE_C_COMPILER bin/arm-linux-androideabi-gcc)
set(CMAKE_CXX_COMPILER arm-linux-androideabi-cpp)
set(CMAKE_FIND_ROOT_PATH /home/name/tmp/my-android-toolchain)
Then write a CMAKE with the codes unders
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchain-armgcc-android.cmake -DBUILD_EXAMPLESERVER=On -DPYTHON_EXECUTABLE=/usr/bin/python ..
It is executed and the follows codes was come out,
-- The C compiler identification is GNU 4.8.0
-- Check for working C compiler: /home/name/tmp/my-android-toolchain /bin/arm-linux-androideabi-gcc
-- Check for working C compiler: /home/name/tmp/my-android-toolchain/bin/arm-linux-androideabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Found Git: /usr/bin/git (found version "1.9.1")
-- Git version: v0.1.0-RC4-31-g1a03d02-dirty
-- CMAKE_BUILD_TYPE not given; setting to 'RelWithDebInfo'.
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
PYTHON_EXECUTABLE
- Build files have been written to: /home/name/open62541/build
in the same file i wrote a command ,make,. Then the errors come out, like
can anyone tall me the reasons? thanks a lot in advance`

Check framework integration on project failed to build

I'm integrating C check framework for my project and I was able to run
autoreconf --install
successfully without any errors.
But when I integrate the C check framework, I'm getting an error and warnings that doesn't make sense.
Here is the error I'm getting
src/Makefile.am:7: warning: variable 'main_LDADD' is defined but no program or
src/Makefile.am:7: library has 'main' as canonical name (possible typo)
tests/Makefile.am:2: error: 'SHELLTESTS_PROGRAMS' is used but 'SHELLTESTSdir' is undefined
tests/Makefile.am:3: warning:variable 'ShellTests_SOURCES' is defined but no program or
tests/Makefile.am:3: library has 'ShellTests' as canonical name (possible typo)
tests/Makefile.am:5: warning: variable ShellTests_LDADD' is defined but no program or
tests/Makefile.am:5:library has 'ShellTests' as canonical name (possible typo)
autoreconf:automake failed with exit status: 1
I've followed the C check example source code, the difference between that and my project is that my class uses other class' methods.
Anyways, this is the Makefile.am under my tests/ that is causing 'havoc' on building the project
TESTS = shelltests
SHELLTESTS_PROGRAMS = shelltests
ShellTests_SOURCES = ShellTests.c $(top_builddir)/src/Shell.h $(top_builddir)/src/Parser.h $(top_builddir)/src/JobControl.h
ShellTests_CFLAGS = #CHECK_CFLAGS#
ShellTests_LDADD = $(top_builddir)/src/libshell.la #CHECK_LIBS#
This is the file structure for my project
src -
Parser.h, Parser.c, Shell.h, Shell.c, Job.h, Job.c, Makefile.am
tests -
ShellTests.c Makefile.am
And this is the code in Makefile.am under src.
lib_LTLIBRARIES = libshell.la
libshell_la_SOURCES = Shell.c Shell.h Parser.h Parser.c JobControl.h JobControl.c
bin_programs = main
main_sources = Main.c
main_LDADD = libshell.la
I followed this user's advice to see if it removes the error: What directory should I use for "error: 'extra_PROGRAMS' is used but 'extradir' is undefined"?
Alas, it does not.
I've tried building the example project - the one that you get when you download under examples, to see if I'm missing anything. But it's not able to build on my machine.
I'm on Mac OS X Mavericks (10.9)
Autoconf 2.69
Automake 1.15
Libtool 2.4.6
Check 0.9.14
I did a couple of things to build this project successfully. I first followed what I outlined here: Check framework example giving me error when running './configure'
Then I followed c check frameworks' naming conventions for the tests source files.
Thanks for the help guys, it gave me some ideas on what the cause of compilation errors.

CMake finds the correct library, but VC++ attempts to link with something else

I have a CMake module to locate FreeGLUT:
FIND_PATH(FREEGLUT_INCLUDE_DIR NAMES GL/freeglut.h)
FIND_LIBRARY(FREEGLUT_LIBRARY NAMES freeglut freeglut_static)
SET(FREEGLUT_LIBRARIES ${FREEGLUT_LIBRARY})
SET(FREEGLUT_INCLUDE_DIRS ${FREEGLUT_INCLUDE_DIR})
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(FreeGLUT DEFAULT_MSG FREEGLUT_LIBRARY FREEGLUT_INCLUDE_DIR)
MARK_AS_ADVANCED(FREEGLUT_INCLUDE_DIR FREEGLUT_LIBRARY)
It works fine and locates freeglut_static.lib when I generate NMake Makefiles on Windows. I'm attempting to statically link FreeGLUT into my DLL:
FIND_PACKAGE(FreeGLUT REQUIRED)
ADD_LIBRARY(vti SHARED ${VTI_SOURCES})
ADD_DEFINITIONS("-DBUILD_VTI=1 -DFREEGLUT_STATIC=1")
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${FREEGLUT_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(vti ${FREEGLUT_LIBRARIES})
My source code builds correctly, but when it gets to the linking stage, VC++ fails with:
LINK : fatal error LNK1104: cannot open file 'freeglut.lib'
Which is strange since freeglut.lib isn't mentioned anywhere that I can see in the generated NMake makefiles. It should be trying to link with freeglut_static.lib, which CMake locates and sets in FREEGLUT_LIBRARIES.
What might be causing this?
This is caused with pragma directives in FreeGLUT code (see freeglut_std.h). Using FREEGLUT_STATIC should really fix that for you, but I think you should pass it to CMake without quotes: ADD_DEFINITIONS(-DBUILD_VTI -DFREEGLUT_STATIC)

Resources