Command to Read file in CMake - c++

Does anyone know of a way to read from a file in CMake, that works in Visual Studio? I have a cross-platform App. in Visual Studio 2017 that compiles with a toolchain in Linux, running in Hyper-V.
I have a simple defined version number in one of my H-files that I would like to read, parse, and append version number to the name of the compiled program (e.g., company_app_1.2.3).
I can't seem to get the file(READ) command to work. Visual Studio is using CMake version 3.11. I assume CMake can do this regardless of the g++ toolchain being used.
Command I tried:
#Read version
-file(READ FileNameHere $sValue)
Visual Studio Error I get:
1>/bin/sh: -c: line 0: syntax error near unexpected token `READ'
The MakeFile does not have a .txt extension, of follow Visual Studio's naming convention, but I would be willing to rename it if there was an advantage (intellisense available, or some more info about the error).
Thanks in advance for any replies.

Related

Eclipse doesn't recognize printf with visual studio build tools 2022 headers

I'm trying to setup local Windows eclipse for remote Linux development. I have encountered problem with eclipse not recognizing simple 'printf()' statement from <stdio.h>. I didn't find any helpful resources ATM to solve this problem, so wondering if you could help me with this.
Header files used from: Visual Studio Build Tools 2022, specifically I'm including this folder for stdio.h file: 'C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041\ucrt'
GCC C++ Compiler as: "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\bin\clang.exe"
Eclipse IDE still highlights this as 'Function cannot be resolved':
I am not sure, but looks like 'printf' function is available at 'stdio.h':
Update:
Eclipse version used:
Using command line build is working as expected:
Program runs as expected:
So the questions are:
What needs to be done so eclipse would recognize 'printf'? e.g. Change MS Win Kits for another compiler header files? Or my eclipse configs are wrong?
If it is not possible for eclipse to recognize 'printf', is there an option to ignore all warnings on 'printf' (not highlight them)?
Let me know would you need anymore details from my side.
After some investigation I think 'clang' is not compatible with eclipse & it's way easier to install & use 'cygwin' for such purposes.
Some references:
https://www.eclipse.org/forums/index.php/t/1092409/
https://www.eclipse.org/forums/index.php/t/1102938/

How can I change MSVC compiler version in a CMAKE project?

Since microsoft broke the compiler (again) in version 14.29.30037 (reported as 19.29.30038.1 by CMAKE for some reason) (look here) I need to go back to 14.28.something. I already downloaded 14.28.29910 through Visual Studio Installer and my Microsoft Visual Studio\2019\Community\VC\Tools\MSVC folder now contains folders with both versions.
However I can't find a way to force CMAKE to use this particular older version. I use CLion CMAKE integration and my settings look like this.
Please note that I've already tried setting Make, C Compiler and C++ Compiler paths to their counterparts in older compiler. This results in CMAKE reporting the desired 19.28.29915.0 compiler version (love the number of different versioning conventions microsoft uses by the way). Despite that, when I try to build the project I get the following:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\yvals_core.h(541): fatal error C1189: #error: STL1001: Unexpected compiler version, expected MSVC 19.29 or newer.
NMAKE : fatal error U1077: 'C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1428~1.299\bin\HostX86\x64\cl.exe' : return code '0x2'
Stop.
I guess this implies that not all parts of the compiler (some includes in this case) were switched to the old version.
If for some reason any piece of my code would help you with answering this question, the repo is available here
Please also note that despite how much I'd love to simply switch to GCC, I cannot because of CUDA support on Windows. I also cannot use different (older) Visual Studio version as the project requires C++ 20. Also, bonus question: How is it possible for a company as relevant as microsoft to literally break an enterprise-class development tool?
The following solution helped me:
Find vcvarsall.bat in the directory with your Visual Studio installation. (I have Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat)
Run vcvarsall.bat <your_arch> -vcvars_ver=<msvc_version>. In your case, put -vcvars_ver=14.28. Example of <your_arch> is amd64. This will set all the environment variables for desired MSVC version.
Start CLion from the command line with the created environment.

Error: No CMAKE_Fortran_COMPILER could be found for Visual Studio 2019 Fortran support

I am using CMAKE to build Open Source Projects (like those are available at GitHub etc.) and I also have installed Visual Studio 2019. There is a problem that CMAKE can not find Fortran compiler in my system whereas I've installed MinGW with Fortran compiler. The error is:
**The Fortran compiler identification is unknown**
**No CMAKE_Fortran_COMPILER could be found.**
How can I solve this problem and make CMAKE be aware of Fortran compiler?
Note: I tried other projects that does not require Fortran compiler and those are built successfully.
Installed software:
CMAKE 3.18.5,
Visual Studio 2019,
MinGW
You need to show where your fortran executable file to CMakeList.txt like
set(CMAKE_Fortran_COMPILER "C:/MinGW/bin/gfortran.exe")
EDIT 2 for Visual studio 2019:
If you want to produce Visual Studio 2019 solution
Download Intel® oneAPI HPC Toolkit here.
If cmake cannot find Fortran compiler add a cmake flag CMAKE_Fortran_COMPILER with the value of ifort.exe path that u installed above. Like %install_path%/Intel/oneAPI/compiler/2021.1.1/windows/bin/intel64/ifort.exe
Configure and generate solution.
EDIT:
Alright, now I understand why you get this error. The Visual Studio generator does not support MinGW gfortran. They are totally separate ecosystems.
Remove your build directory and create a fresh one. Then use cmake .. -G "MinGW Makefiles" instead. I tried from CLI prompt of msys and successfully obtained the libraries from Windows machine.
If you are using Intel processors, you should first set-up Fortran environment for Visual Studio 2019
Better to check compilation guide and some troubleshooting Fortran Integration Issues with visual studio

Unable to debug c++ code in Visual Studio Code

I've just installed Visual Studio Code v1.7.1 to write my c++ code in for my degree. My programming level is pretty basic, but I like what I've seen of VS Code so far, save for the fact that I've no idea how to debug or build my code.
I've looked at a few questions on the topic, such as: How do I set up VSCode to compile C++ code?
and have tried implementing some of these into the tasks.json file, including the make command and the g++ command, but when I try and execute it I get the error:
'make' is not recognized as an internal or external command,
operable program or batch file.
or something similar. When I try running the debugger, it tells me to set up the launch.json file, so I entered the path of my file where it says 'program' as the instructions imply, I get the error
Unknown Error: 0x800700c1
I'm just looking for the simplest option that will allow me to write and debug code in vs code, in a manner similar to using the full version of visual studio, but without the heavy project filing system or the 10 minute loading times. Any help would be greatly appreciated.
What I deduce from your question and the comments is that you have no actual C++ compiler/buildchain installed. There are multiple compilers available (like minGW, GCC).
However, what Microsoft recommends is the following:
Installing C/C++ build tools
To obtain your set of C/C++ compilers on Windows you can grab the Visual C++ build tools SKU. By default these tools are installed at ‘C:\Program Files (x86)\Microsoft Visual C++ Build Tools’.
I have not tested, nor ever worked with the Visual C++ compiler, but it's probably the easiest way to get started right away.

How run clang from command line on Windows?

At the Going Native conference last week, Chandler Carruth announced the existence of prebuilt binaries for running clang on windows. The same information is in a blog post here. The intended audience for this is users of Visual Studio, but I want to run clang from the command line.
I ran the installer and added the LLVM bin directory to my path, but when I try to compile "Hello world", I get this:
C:\>clang hello.cpp
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
I can't find any information on how to configure things to run clang on Windows, and I'm guessing that after I figure out how to tell clang where to search for standard library headers, I'll have to tell it where to look for libraries to link with. Can somebody walk me through the setup step by step or point me to such a walkthrough?
This is a old question, and a lot has changed since then. Given this is a common problem when trying Clang on Windows, it deserves an updated answer.
As of 2017, with the LLVM 3.9.1 build for Windows, you need the following to be able to invoke clang from your shell.
VC++ Build Tools
We still do not have a libc++ port for Windows, so Clang uses the VC++ libraries as well as the VC++ linker.
So first of all you need the VC++ Build Tools on your system. Do note you already have those installed if you happen to have the Visual C++ IDE.
Environment Variables
You need to tell Clang where to find the build tools and its libraries.
Option 1 (vcvarsall.bat)
This is the easiest and standard option.
Run
> "%VS140COMNTOOLS%../../VC/vcvarsall.bat" amd64
Replacing amd64 with your target architecture on Clang, which may be x86, amd64 or arm. You may replace %VS140COMNTOOLS% as well if you have a different version of the VC++ toolset.
As a shortcut, you could run the Visual C++ Command Prompt instead of cmd+vcvarsall, since you need to call this batch for every command prompt you open.
Now you are able to enjoy Clang.
Option 2 (Manually)
In case you cannot run vcvarsall.bat or want to automate this process, welcome, I had the same need.
All of the following environments variables are set automatically by vcvarsall.bat, so you can run that and take your machine values from there. I'll give mines as examples and in the hope it's the same elsewhere.
Set the INCLUDE environment variable to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE;C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt;C:\Program Files (x86)\Windows Kits\8.1\include\shared;C:\Program Files (x86)\Windows Kits\8.1\include\um;C:\Program Files (x86)\Windows Kits\8.1\include\winrt;
Set LIB to C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64;. Do note the architecture specific components!
For the build tools, you can either have the tools on PATH or setup the VCINSTALLDIR environment variable. Clang will try both, favoring VCINSTALLDIR.
Set VCINSTALLDIR to %VS140COMNTOOLS%../../VC or add %VS140COMNTOOLS%../../VC/bin/amd64 to your PATH.
Footnote
This is all very under documented, so the requirements may change at any time, but the Clang MSVC driver is trying to automate this as much as possible, by querying the Windows Register and many other tricks, so none of this may be necessary anymore in the future.
If you are not restricted to use Microsoft compilers. You can use clang with MinGW-w64. Just install the latest version of llvm binary for Windows and MinGW-w64.
You can use the following code to compile your source file
clang++ -target x86_64-pc-windows-gnu test.cc -o test.exe
With Clang for Windows 5.0.0 (64 Bit) (latest versions available here; you want LLVM-x.y.z-win64.exe) and Visual Studio 2017 Community Edition or Build Tools installed in their default setup paths (including the latest/matching Windows SDK):
C:\>clang --version
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
I've made good experiences using clang-cl (clang-cl.exe == clang.exe --driver-mode=cl) which does find all the necessary msvc library/include dependencies automatically:
C:\>clang-cl hello.cpp
Or to compile as 32 or 64 Bit application:
C:\>clang-cl -m32 hello.cpp
C:\>clang-cl -m64 hello.cpp
Alternative
See Arvid Gerstmann's Blog: Using clang on Windows.
References
How do I tell CMake to use Clang on Windows?
Building a x86 application with CMake, Ninja and Clang on x64 Windows
How do I tell CMake to use Clang on Windows?
Here is what I did to use the clang compiler from the terminal on Windows 10:
I downloaded and installed the Build Tools for Visual Studio 2022. This installs and opens the Visual Studio Installer.
In the Visual Studio Installer I selected three things:
Desktop development with C++ from the Workload tab
C++ Clang Compiler for Windows (13.0.1) from the Individual Components tab.
C++ Clang-cl for v143 build tools (x64/x86) from the Individual Components tab.
Then I added the path to clang.exe and clang++.exe to my PATH environment variable. For me the path was C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\Llvm\x64\bin.
After doing this, I was able to use the clang compiler from the terminal. To compile a C program, go to the source directory and type;
clang *.c
To compile a C++ program, use instead;
clang++ *.cpp
I hope this information is useful to others.