Cmake failing at linking step of sample program - c

I have been searching for an answer to my problem for a while now and I haven't found any useful information yet. I'm hoping someone here can help me out. I'm trying to get CMake working on a Windows 10 machine through MSYS2 and it's proving to be a difficult job for me to say the least, lol.
I've managed to figure out how to install msys2 and then install the build toolchain and get it running in the correct environment so I get some sort of output, but now I'm stuck at a step in the Ninja build process and I can't get past it...
Here is the pertinent section of the error log:
[2/2] Linking C executable cmTC_d5d92.exe
FAILED: cmTC_d5d92.exe
cmd.exe /C "cd . && C:\msys64\mingw64\bin\cc.exe CMakeFiles/cmTC_d5d92.dir/testCCompiler.c.obj -o cmTC_d5d92.exe -Wl,--out-implib,libcmTC_d5d92.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
The system cannot find the path specified.
ninja: build stopped: subcommand failed.
And I can't figure out what path it cannot find. All of the .obj, .a, etc files with the "cmTC_*" are temporary because they change every time I run this command, so I can't really check to see if any of those are invalid, but I don't think they would be. I've checked the .exe file in the command and it exists on my system... So I don't know where to go from here. I'd imagine that permissions or configuration might be the issue, but I don't know where to look, or what to check next.
BTW: this is the command I am running:
cmake -G Ninja ..
Thanks for the help.

Related

GCC: No such file or directory

I made a fresh project and have been stuck in this specific issue for hours. I am using a shell script (.bat) file to compile and build the project. If I use no libraries, it compiles and runs fine. However, when I try to add SDL2 I get the issues described in the title. I was using Visual Studio Code, set up everything correctly in tasks.json and the issue persisted. I moved to 4code and the issue remains.
This is my build.bat
#echo off
if not exist build mkdir build
pushd build
gcc ..\source\main.c -o main.exe
gcc -L ..\external\SDL2\include
popd
I added the library in the "external" folder. It tells me it can't find the SDL.h file. I am confused because the file is in ..\external\SDL2\include
I never coded in C, so all this compiler business is new to me. I've searched the web for hours and can't find a solution. I'd appreciate any help.
Directory-adding options must be used with compilation command.
gcc ..\source\main.c -o main.exe is a compilation command without directory adding
and gcc -lSDL2 and gcc -L ..\external\SDL2\include are not compilation commands
because they don't include what to compile.
The 3 gcc invokations should be one invokation:
gcc -L ..\external\SDL2\include ..\source\main.c -o main.exe -lSDL2

How to modify CMakeList.txt: library found, but there are linking errors

I am creating a C library which is to be built with cmake, using Mac OS for development. In the CMakeList.txt, I have the following
#htslib
find_package(htslib REQUIRED)
include_directories(${HTSLIB_INCLUDE_DIR})
target_link_libraries(projectname ${HTSlib_LIBRARIES})
which outputs upon cmake ..
Found hstlib
However, upon make, I'm getting linker errors:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So...it can find the library, and the library is definitely installed with sudo make install, but there are linking errors only with this library.
(1) I'm guessing that find_package(htslib REQUIRED) is finding something else. How do I find out what?
(2) How do I explicitly write in CMakeList.txt to find the library which I know has been installed correctly?
Use VERBOSE=1 make to see the linker output. Search for -lhtslib
Read the documentation for the specific Find<LIB>.cmake.
Your specific questions:
"How do I find what CMake found": Use cmake-gui or ccmake. They both show the same info, but one is a GUI and the other is a Curses interface. In the advanced mode ("t" on ccmake) you will find all the variables for the searched packages. Additionally, you may use MESSAGE(STATUS "Found htslib at: ${htslib_LIBRARIES}").
"How to explicitly write in CMakeLists.txt where the library is?" Please, do not do that! CMake is meant for abstracting exactly this kind of information away. You have two options, first the good one: configure cmake on the command line (or in the GUIs mentioned above) to get a CMAKE_MODULES_PATH or a more specific hint to the library -D htslib_PATH=/usr/local/.../ (pointing to the dir where libhts.dylib resides). The worse solution would be to provide a HINT to find_package. find_package(htslib REQUIRED PATH /usr/local/lib) or find_package(htslib REQUIRED HINT /usr/local/lib /some/second/path/where/it/may/be).
Solution
Your linked project has a custom FindHTSlib.cmake link. This one uses pkg_config to configure the library. To replicate your problem, I used brew to install htslib. The pkg-config file can be found (for me, but brew info htslib tells you) under /usr/local/Cellar/htslib/1.8/lib/htslib.pc. So, let's give CMake the required hint.
I couldn't test this, because for me it found the htslib package directly with no further hints.
git clone https://github.com/D-Lo/bamdb # I am using version f5f03d0
mkdir -p bamdb/build; cd bamdb/build
brew install ck htslib lmdb
cmake .. # -G Ninja recommended, but needs brew install ninja
make # lot's of missing symbols
I would recommend to change in CMakeLists.txt the minimum required version of CMake from 2.8 to 3.10 (or at least 3.6).
This is the error I get:
[ 62%] Linking C shared library libbamdb.dylib
/usr/local/Cellar/cmake/3.11.1/bin/cmake -E cmake_link_script CMakeFiles/libbamdb.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/cc -Wall -g -std=gnu99 -fPIC -dynamiclib -Wl,-headerpad_max_install_names -o libbamdb.dylib -install_name #rpath/libbamdb.dylib CMakeFiles/libbamdb.dir/src/bam_api.c.o CMakeFiles/libbamdb.dir/src/bam_lmdb.c.o CMakeFiles/libbamdb.dir/src/bamdb.c.o
Undefined symbols for architecture x86_64:
"_bam_destroy1", referenced from:
_get_bam_row in bam_api.c.o
_deserialize_func in bam_lmdb.c.o
This can be fixed by adding the following line in the CMakeLists.txt, after the line add_library(libbamdb ${SOURCES}):
target_link_libraries(libbamdb ${LIBS})
Some further notes: you now have a library with a main function. This is because ${SOURCES} is used to build the executable and the library. That can have unexpected side effects. Unless it's needed, don't do it.

Determining if the C compiler works failed with the following output

I am trying to use NDK to link a C project to Android Studio. I get the following Cmake error. I get the same error with any project that uses NDK.
Determining if the C compiler works failed with the following output:
Change Dir: C:/Users/Alex/Desktop/android-ndk/audio-echo/app/.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/CMakeTmp
Run Build Command:"C:\Users\Alex\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\ninja.exe" "cmTC_52340"
[1/2] Building C object CMakeFiles/cmTC_52340.dir/testCCompiler.c.o
[2/2] Linking C executable cmTC_52340
FAILED: cmd.exe /C "cd . && C:\Users\Alex\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe --target=armv7-none-linux-androideabi --gcc-toolchain=C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/sysroot -isystem C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Wl,--exclude-libs,libgcc.a --sysroot C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -LC:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--gc-sections -Wl,-z,nocopyreloc -pie -fPIE CMakeFiles/cmTC_52340.dir/testCCompiler.c.o -o cmTC_52340 -lm && cd ."
The system cannot find the path specified.
ninja: build stopped: subcommand failed.
I am using Windows 10 and Android Studio 2.3.3.
I can successfully run the same program on another Windows 10 machine.
I would love to understand at least what part of this is failing. Any input is appreciated!
The error "The system cannot find the path specified." is issued by cmd.exe (that is, the Windows 10 command interpreter). CMake is invoking cmd.exe and asking it to run C:\Users\Alex\AppData\Local\Android\sdk\ndk-bundle\toolchain‌​s\llvm\prebuilt\wind‌​ows-x86_64\bin\clang‌​.exe, but cmd.exe cannot find it, presumably because it doesn't exist. This suggests that NDK may not be installed properly, or at all. Use Windows Explorer or cmd to look for the existence of this file; if it doesn't exist, your tools aren't installed properly. See where things went awry; for example, if C:\Users\Alex\AppData\Local\Android\sdk\ndk-bundle\toolchain‌​s\llvm\prebuilt\wind‌​ows-x86_64\bin exists and has other files in it, perhaps something got corrupt. If you can't even find ndk-bundle, NDK is probably not installed at all.
If the file exists, try executing it; go into cmd and run C:\Users\Alex\AppData\Local\Android\sdk\ndk-bundle\toolchain‌​s\llvm\prebuilt\wind‌​ows-x86_64\bin\clang‌​.exe directly. If it works, there is something wrong with your CMake environment (not sure what), but I'm guessing it won't work.

how do i use cmockery in my projects

I was searching for a way to create mocking objects with c-code until I stumbled upon cmockery.
For me it seems to be the best mocking software available since it doesn't have a lot of dependencies.
I'm working in ubuntu and downloaded the tarball cmockery from https://code.google.com/p/cmockery/downloads/list
I ran the ./configure, make and make install.
I am able to execute the given examples but I just can't figure out how to get it working on my own projects. I had a look at the configure and makefile to try and find out how they did it, but that was no success. I think it's the linking that's causing my problems.
Files of cmockery can be find at:
/usr/local/include/google/cmockery.h
/usr/local/lib/libcmockery.la
/usr/local/lib/libcmockery.a
/usr/local/lib/libcmockery.so.0.0.0
/usr/local/lib/libcmockery.so.0
/usr/local/lib/libcmockery.so
I tried copying the example files calculator.c and calculater_test.c to a separate directory and compile them there.
This is what I did:
gcc -c -o calculator.o calculator.c
gcc -c -o calculator_test.o calculator_test.c -I /usr/local/include/google/
gcc -o run *.o -L /usr/local/lib/
At the last step I got a lot of undefined references to all functions specific to cmockery and the error:
collect2: error: ld returned 1 exit status
I guess I'm messing things up with the linker but I can't find anywhere how it should be done correctly.
You are missing -lcmockery:
gcc -o run *.o -L /usr/local/lib/ -lcmockery

Can not find linux/modversions.h

I am trying to install the driver for a serial device, and when I run the installation executable I get this error:
cc -DLINUX -c -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -m64 -mcmodel=kernel -I/usr/src/linux-3.8.0-27-generic/include -I/usr/src/linux-2.4/include -I/usr/src/linux/include -D__SMP__ npreal2.c
npreal2.c:40:31: **fatal error: linux/modversions.h: No such file or directory**
compilation terminated.
I don't find any solutions to this after searching the forums. I noticed that there is a modversions.h in the /usr/src/linux-3.8.0-27-generic/include/config , but not in the linux folder.
Please help!
Try passing -I /usr/src/linux-3.8.0-27-generic/include/config as an argument to make?
or
Check if the header is a part of a certain package and update the package.
You can compile modversions on your system by navigating to the linux directory (usually usr/src/linux). Inside the linux source directory, there should be a file called Rules.make. Inside this make file are build commands for making modversions.h. You can make it by running:
make update-modverfile
Now, while this will make the modversions.h library, if you compile it with a newer compiler than the libraries that this file relies on, many times you will get an error when trying to run a program that uses this header. This then turns into a nightmare.
Another method, I tried it successfully with Xubuntu 13.10:
Open /etc/default/grub
Add this Line and save it.
GRUB_CMDLINE_LINUX="CONFIG_MODVERSIONS=true"
reboot
(no, sudo update-grub,ok)
open a terminal window, enjoy.
locate modversions.h
(Please don't forget modversion'S')

Resources