A cmake macro for platform dependent setting variable value
Inside the macro you need to dereference name
for obtain the value of the parameter:
set(${name} ${msvc_val})
By current code
set(name ${msvc_val})
you just define the variable with the name "name".
NDK support with different Product Flavour
Finally I a solution.
Here is my cpp file code
Java_com_de_demo_ndk2_MainActivity_stringFromJNI(
JNIEnv *env,
jobject jobject1, jstring jstring1) {
std::string hello;
const char *nativeString1 = env->GetStringUTFChars( jstring1, 0);
if (strcmp(nativeString1, "demo") == 0) {
hello = "Hello from demo C++";
} else if (strcmp(nativeString1, "live") == 0) {
hello = "Hello from live C++";
}
return env->NewStringUTF(hello.c_str());
}
I am passing flavor value from java code here is my code for java file.
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
String value = BuildConfig.FLAVOR;
String ndkValue = stringFromJNI(value);
tv.setText(ndkValue);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
* @param value
*/
public native String stringFromJNI(String value);
}
Now i can get whatever text i want according to selected flavor.
CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
Those error messages
CMake Error at ... (project):
No CMAKE_C_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also ".../CMakeFiles/CMakeOutput.log".
See also ".../CMakeFiles/CMakeError.log".
or
CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.
Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
...
-- Configuring incomplete, errors occurred!
just mean that CMake was unable to find your C/CXX compiler to compile a simple test program (one of the first things CMake tries while detecting your build environment).
The steps to find your problem are dependent on the build environment you want to generate. The following tutorials are a collection of answers here on Stack Overflow and some of my own experiences with CMake on Microsoft Windows 7/8/10 and Ubuntu 14.04.
Preconditions
You have installed the compiler/IDE and it was able to once compile any other program (directly without CMake)
- You e.g. may have the IDE, but may not have installed the compiler or supporting framework itself like described in Problems generating solution for VS 2017 with CMake or How do I tell CMake to use Clang on Windows?
You have the latest CMake version
You have access rights on the drive you want CMake to generate your build environment
You have a clean build directory (because CMake does cache things from the last try) e.g. as sub-directory of your source tree
Windows cmd.exe
> rmdir /s /q VS2015
> mkdir VS2015
> cd VS2015Bash shell
$ rm -rf MSYS
$ mkdir MSYS
$ cd MSYSand make sure your command shell points to your newly created binary output directory.
General things you can/should try
Is CMake able find and run with any/your default compiler? Run without giving a generator
> cmake ..
-- Building for: Visual Studio 14 2015
...Perfect if it correctly determined the generator to use - like here
Visual Studio 14 2015
What was it that actually failed?
In the previous build output directory look at
CMakeFiles\CMakeError.log
for any error message that make sense to you or try to open/compile the test project generated atCMakeFiles\[Version]\CompilerIdC
|CompilerIdCXX
directly from the command line (as found in the error log).
CMake can't find Visual Studio
Try to select the correct generator version:
> cmake --help
> cmake -G "Visual Studio 14 2015" ..If that doesn't help, try to set the Visual Studio environment variables first (the path could vary):
> "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
> cmake ..or use the
Developer Command Prompt for VS2015
short-cut in your Windows Start Menu underAll Programs
/Visual Studio 2015
/Visual Studio Tools
(thanks at @Antwane for the hint).
Background: CMake does support all Visual Studio releases and flavors (Express, Community, Professional, Premium, Test, Team, Enterprise, Ultimate, etc.). To determine the location of the compiler it uses a combination of searching the registry (e.g. at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\[Version];InstallDir
), system environment variables and - if none of the others did come up with something - plainly try to call the compiler.
CMake can't find GCC (MinGW/MSys)
You start the MSys
bash
shell withmsys.bat
and just try to directly callgcc
$ gcc
gcc.exe: fatal error: no input files
compilation terminated.Here it did find
gcc
and is complaining that I didn't gave it any parameters to work with.So the following should work:
$ cmake -G "MSYS Makefiles" ..
-- The CXX compiler identification is GNU 4.8.1
...
$ makeIf GCC was not found call
export PATH=...
to add your compilers path (see How to set PATH environment variable in CMake script?) and try again.If it's still not working, try to set the
CXX
compiler path directly by exporting it (path may vary)$ export CC=/c/MinGW/bin/gcc.exe
$ export CXX=/c/MinGW/bin/g++.exe
$ cmake -G "MinGW Makefiles" ..
-- The CXX compiler identification is GNU 4.8.1
...
$ mingw32-makeFor more details see How to specify new GCC path for CMake
Note: When using the "MinGW Makefiles" generator you have to use the
mingw32-make
program distributed with MinGWStill not working? That's weird. Please make sure that the compiler is there and it has executable rights (see also preconditions chapter above).
Otherwise the last resort of CMake is to not try any compiler search itself and set CMake's internal variables directly by
$ cmake -DCMAKE_C_COMPILER=/c/MinGW/bin/gcc.exe -DCMAKE_CXX_COMPILER=/c/MinGW/bin/g++.exe ..
For more details see Cmake doesn't honour -D CMAKE_CXX_COMPILER=g++ and Cmake error setting compiler
Alternatively those variables can also be set via
cmake-gui.exe
on Windows. See Cmake cannot find compiler
Background: Much the same as with Visual Studio. CMake supports all sorts of GCC flavors. It searches the environment variables (CC, CXX, etc.) or simply tries to call the compiler. In addition it will detect any prefixes (when cross-compiling) and tries to add it to all binutils of the GNU compiler toolchain (ar
, ranlib
, strip
, ld
, nm
, objdump
, and objcopy
).
Why is cmake file GLOB evil?
The problem is when you're not alone working on a project.
Let's say project has developer A and B.
A adds a new source file x.c
. He doesn't changes CMakeLists.txt
and commits after he's finished implementing x.c
.
Now B does a git pull
, and since there have been no modifications to the CMakeLists.txt
, CMake isn't run again and B causes linker errors when compiling, because x.c
has not been added to its source files list.
2020 Edit: CMake 3.12 introduces the CONFIGURE_DEPENDS
argument to file(GLOB
which makes globbing scan for new files: https://cmake.org/cmake/help/v3.12/command/file.html#filesystem
This is however not portable (as Visual Studio or Xcode solutions don't support the feature) so please only use that as a first approximation, else other people can have trouble building your CMake files under their IDE of choice!
Get OS name with C [Linux, portable for distros: Centos, Debian, Fedora, OpenSUSE, RedHat, Ubuntu]
The uname
system call gives you the generic system type (Linux
in all your cases) in the sysname
field, but it also gives you additional data in the release
, version
, and machine
fields. The release
field will give you the kernel version, and the version
field will give you the general system version, which will be different for all the various linux variants you mention.
What is the best way of CMake in a Middle-sized Project?
The topic is huge, but in short my personal recommendation. For a middle project I assime a component model should be already applied. Then reasonable then is, to have component directories with their onwn CMakeLists.txt which are referenced by the top-level CMakeLists.txt via add_subdirectory(). Each component - a separate library (I like static ones).
For the component folders I find reasonable to hide all internal stuff (aka implementation and private headers, ...) under a private
sub-directory to do not be exposed to the outside. Then, in the top component directory you have only headers which are to be used by the others. In the private directory you can mix sources and headers - this is only a matter of the taste for mid projects. And the private directory can also be decomposed if the component is large. But then you need to decide either to add all artifacts to the single CMakeLists.txt of the component, or to have sub-libraries. But in that case the users should link to them individually instead to link to the component's library only.
In the best case, the folder structure should follow the dependencies structure and form a tree-view build system, where the components have as less knowlege about internals of the other components as possible. In that case you will have a good configurability and flexibility in case of possible refactorings. In the other words, the design of the build system seems to me similar to the class design in C++ - same principles.
The real (target) build directory where you run cmake can be located anywhere, normally outside of the source directory. A good place for it could be a RAM disc if you enough memory. Then for the clean build you need just to remove it, that's it. But the source and the build itself have no dependency from its location.
Ah yes, one more hint. My recommendation would be to include headers by the path starting from the component directory like #include "SomeHeader.hpp"
which is located as ComponentX/SomeHeader.hpp. Then the CMakelists.txt is used to do the ComponentX
directory known to your component. This means, the paths to the headers are not hardcoded in the source files. This brings some limitation like unique file names, but makes changes to the components location much easier.
Hope this anyhow helps.
Related Topics
Packaging Proprietary Software for Linux
Svg to PDF on a Shared Linux Server
The Meaning of Real, User, and Sys in Output of Linux Time Command
Using Environment Variables in Curl Command - Unix
How to Read the Last Line of a Text File into a Variable Using Bash
Custom Git Command Autocompletion
Finding Original MAC Address from Hardware Itself
Rtnetlink Answers :No Such File or Directory Error
Maven: Bash Mvn Permission Denied
Vim: Access to System Clipboard via Ssh - Linux to Os X
How to Change Version String of the Kernel
Linux/Perl: Additional Output Buffers Other Than Stdout and Stderr
How to List the Files in a Zip Archive Without Decompressing It
Why Can't We Use C Standard Library Functions in Kernel Development