How to have cmake chache file not in c++ source directory - linux

Maybe stupid question, but how do I change directory where cmake files gonna be created, so I can keep my project kinda cleaner.
For example when I run cmake -S. -B./src it is not gonna create files and directories like cmakeChache, cmakefiles and other in my source directory.
Btw I running linux.

I understood how to do it. I just had CMakeLists.txt file and my source c++ code in another directories and I don't know how to set source path to CMakeLists.txt file and c++ source in the same time so I just moved CMakeLists.txt file to source directory and now I don't have to set build path to path of my source code.

The syntax is:
cmake -S <PATH/TO/SOURCE_FILES> -B <PATH/TO/BUILD_OUTPUT>
cmake --build <PATH/TO/BUILD_OUTPUT> --target all
It is recommended that <PATH/TO/BUILD_OUTPUT> is not a sub directory of <PATH/TO/SOURCE_FILES>.
It should be parallel to the sources:
+-PATH
+-TO
+-BUILD_OUTPUT
+-otherstuff
+-SOURCE_FILES
Note: Most CMS(e.g. git) can be configured to not consider generated files.

Related

WSL + Scons: No such file or directory

I'm using WSL and SCons for cross compiling with arm-none-eabi-gcc. When I try to build I get: "scons: *** [target] No such file or directory", where "target" is the name of the object file to build. The same build is working on our build machine (Arch Linux) and if I compile manually it also work fine. So obviously SCons can't find the source code, but why?
I suspect that scons is not able to find 'sh' rather than the object file. Make sure that you aren't editing env['PATH'] anywhere in your build scripts?

CMake installs to bin/lib but app can't find lib

I'm building a multi-binary project with cmake and deploying in Debian. CMakeLists.txt reduces down to something like this:
add_library(mylib SHARED lib.cpp) #creates libmylib.so
add_executable(myapp main.cpp)
target_link_libraries(myapp my-lib)
install(TARGETS mylib myapp
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
)
If I install this to (-DCMAKE_INSTALL_PREFIX=/usr) then I have no problem. But if I install to somewhere else like (-DCMAKE_INSTALL_PREFIX=/opt/myapp, or even -DCMAKE_INSTALL_PREFIX=/usr/local), then I have a problem.
When I run $ /opt/myapp/bin/myapp my application can't find the .so.
I could deploy a script with myapp which sets:
#!/bin/sh
export LD_LIBRARY_PATH=${CMAKE_INSTALL_PREFIX}/lib:$LD_LIBRARY_PATH
exec ${CMAKE_INSTALL_PREFIX}/bin/myapp $*
But this feels like a hack. Plus, the script would need to be generated at configure time with ${CMAKE_INSTALL_PREFIX}/lib.
I imagine that there's a more native way to handle this which lets me simply execute my application from /opt or /usr/local after installation. It would preferably handle this at configure, compile, or install time instead of just before runtime and preferably wouldn't require someone to modify their ~/.bashrc or ~/.profile.
Could you please tell me if there is some way to deploy the standard bin,lib structure in linux to arbitrary paths without the need for pre-runtime scripting?
You should:
use rpath (Unix) or loader_path (MacOS)
or install it in regular system path (/usr/lib or /usr/local/lib etc...)
or use LD_LIBRARY_PATH.
Example to set RPATH:
if(APPLE)
set(CMAKE_INSTALL_RPATH "#loader_path/../lib;#loader_path")
elseif(UNIX)
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")
endif()
note: on macos you should now use #rpath
note2: on macos you can use otool -l and otool -L to introspec.
note3: you can use ldd lib.so and objdump -p lib.so on GNU/Linux.
Note: Prefer to use GNUInstallDirs
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

Spike: error while loading shared libraries: libriscv.so

I tried to execute spike this way by navigating to the folder the executable is in:
cd ~/riscv-tools/riscv-isa-sim/build
./spike
I get this error message:
./spike: error while loading shared libraries: libriscv.so: cannot open shared object file: No such file or directory
It is significant that the file it claims to not find is in the same directory as the spike executable (in the build directory) - any help?
The dynamic linker generally looks for shared libraries in predefined system directories such as /lib, /usr/lib as specified by ldconfig.
You can tell the linker to search in other directories with LD_LIBRARY_PATH:
LD_LIBRARY_PATH=. ./spike
The usual way is to execute Spike it to execute it from the installed location, e.g. install it like this:
cd riscv-isa-sim
mkdir build
cd build
../configure --prefix=$HOME/local/riscv/spike
make
make install
And then execute it:
~/local/riscv/spike/bin/spike ...
No need to mess around with your LD_LIBRARY_PATH then (which really should be avoided, if possible).

node.js could not use lib in /usr/local/lib

I'm using a node module png which use libpng. After installing libpng, I find some libs in /usr/local/lib. I require the png module:
var png = require('png')
It complained that libpng16.so could not be found.
Error: libpng16.so.16: cannot open shared object file: No such file or directory
But libpng16.so.16 does exist in /usr/local/lib. Then I copy all libpng* to /usr/lib and run code above again, no error for this time!
My question: how could I let Node search libs in /usr/local/lib?
Thanks!
This is a Linux "installing libraries" issue, not a node.js issue (I was confused by the same thing & landed here looking for ideas).
make install will typically copy the library to /usr/local/lib and output some boilerplate suggesting that you modify LD_LIBRARY_PATH or update the ld config. But it doesn't do this for you.
(One thing that can make this more confusing is that the compiler toolchain will search /usr/local by default, so any dependent libraries will compile/link fine.)
Running ldconfig (/sbin/ldconfig) as root or with sudo will update the run-time linker cache, and fix the problem. If not, check that at least one of /etc/ld.so.conf or any of the files in /etc/ld.so.conf.d/ contains the line '/usr/local/lib'.
For more information, run man ldconfig

CMAKE auto header file dependency

Question is similar to this question
Handling header files dependencies with cmake
I have sample program dir having main.c main.h and CMakeLists.txt
main.h contents are
#ifndef MAIN_H
#define MAIN_H
int t=3;
int y=2;
#endif
main.c contents are
#include <main.h>
#include<stdio.h>
int main(){
printf("%d apple",t);
}
and CMakeLists.txt
PROJECT( test )
AUX_SOURCE_DIRECTORY(. test_SRCS)
include_directories(.)
ADD_EXECUTABLE (main ${test_SRCS})
but cmake is not rebuilding main.c on modification of header file.
I want it to auto-generate header file dependency.
Is it possible using cmake ?
if not is there any other tool which can do that ?
As mentioned in my comment, I have tried out your example and things were working fine: if main.h was modified then main.c would be recompiled.
My installation of CMake (version 2.8.0) told me to add
cmake_minimum_required(VERSION 2.8)
to the CMakeLists.txt file, but that is all of the adjustments I needed.
Answering this for others that google search...
I ran into this problem with one of my projects. As it turns out I added the header to the cpp file after running cmake. Re-running cmake fixed the problem. If you run into this, try that and see if it fixes the issue.
From the cmake 2.8.0 documentation of AUX_SOURCE_DIRECTORY:
It is tempting to use this command to avoid writing the list of source
files for a library or executable target. While this seems to work,
there is no way for CMake to generate a build system that knows when a
new source file has been added. Normally the generated build system
knows when it needs to rerun CMake because the CMakeLists.txt file is
modified to add a new source. When the source is just added to the
directory without modifying this file, one would have to manually
rerun CMake to generate a build system incorporating the new file.
Why do you want to avoid creating a list of files? Such lists generally do not change frequently.

Resources