Mixed language C++, C and Fortran compilation with Cmake - gcc

Using g++, gcc and gfortran on GNU/Linux, I've written a simple script to compile and link together a number of source code files written in C++, C and Fortran. Here are the complete contents of the script. This script has been tested, and works well.
g++ -c test-Q.cpp -I./boost/boost_1_52_0/ -g
gcc -c paul2.c -g
gcc -c paul2_L1.c -g
gcc -c paul6.c -g
gcc -c paul6_L1.c -g
gcc -c fit_slope.c -g
gfortran -c getqpf.F -g
g++ -o test-Q test-Q.o paul2.o paul2_L1.o paul6.o paul6_L1.o fit_slope.o getqpf.o -g -lgfortran
To make this more cross-platform, I would like to re-write the script using Cmake. How might I handle mixed-language compilation?
The following test script listed below does not work, and will only selectively compile some of the files.
Is there perhaps another cross-platform build process that might be better suited for this type of compilation?
cmake_minimum_required (VERSION 2.6)
project (q-test)
include_directories(/media/RESEARCH/SAS2-version2/test-Q/boost/boost_1_52_0)
add_executable( q-test
test-Q.cpp
paul2.c
paul2_L1.c
paul6.c
paul6_L1.c
fit_slope.c
getqpf.F
) # end

You need to enable Fortran for the project like this:
project (q-test C CXX Fortran)
Also, you might want to use find_package(Boost) instead of hard coding an include path.

Related

Specifying both -O2 and -O3 at same time

Some programs already uses -O2 flag, if I use -O3 flag, the program compiles with both -O2 and -O3 as shown by the task manager or by /proc/PID/cmdline.
For example, I'm using a Linux kernel built with Clang and full LTO. Even though I have these lines in the dkms configuration:
# /etc/dkms/framework.conf
export LLVM=1
export CC=clang
export CFLAGS="-O3 -march=native"
Now DKMS modules compile with both -O2 and -O3 flags. In this case, which flag is actually used?
For gcc, see https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Optimize-Options.html#Optimize-Options
If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.
clang doesn't document its option syntax as extensively, but it generally tries to be compatible with gcc, so it should be the same in this regard.

How to compile kernel with debug symbols for my own OS?

Hi I'm a beginner and trying to write a Linux like kernel.
I use Qemu as my emulator and currently debug in a assembly level.
However, by previous experience, I can debug Linux kernel with Qemu at source code (.c files) level.
So I would like to ask if I can do it with my own kernel, so that I can debug it with efficiency.
In order to provide more info, the following is my compilation script:
# Complie head.S
gcc -E ./PysicCodes/head.S > head.s
as --64 -o head.o head.s
gcc -E ./PysicCodes/AP_Boot.S > AP_Boot.s
as --64 -o AP_Boot.o AP_Boot.s
# Compile main program
gcc -mcmodel=large -fno-builtin -fno-stack-protector -m64 -c ./PysicCodes/*.c
# Interrupt hander requires general register only(since no XMM,SEE registers are saved)
gcc -mcmodel=large -fno-builtin -fno-stack-protector -m64 -mgeneral-regs-only -c ./PysicCodes/g_reg_only/*.c
# Linkage: Must put head.o at first, so that kernel start at head.o
ld -b elf64-x86-64 -z muldefs -o system head.o 8529A.o ACPI.o AP_Boot.o APIC.o cpu.o INT.o keyboard.o main.o Mem.o PCI.o Printk.o SMP.o Task.o Time.o TSS.o fat32.o -T ./PysicCodes/Kernel.lds
# Dump kernel
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary system Kernel.bin
Thanks for anyone who would spend time on helping. Any extra information needed, please comment.
Currently I tried to add -ggdb3 as the compiling and linking options.
Furthermore, using the compiled object as the option for gdb
It simply worked.
In addition, adding "miDebuggerServerAddress": "localhost:1234" into Vscode's "launch.json" file, it actually can connect to Qemu and debug c code in vscode.
However only 1 problem, that vscode will run Qemu at start, so I have to press pause button as soon as I can after start debugger, and using "-exec" to put a hardware break-point.
There is an issue on git and currently haven't see an answer.

Can a CMD batch file or PowerShell script execute multiple g++ commands?

I like using both Linux and Windows for my C and C++ coding and I prefer using the command line to compile my programs. I can run make on Linux, which is fine. But on Windows, now that I'm working with classes and have to compile multiple files, I find it a chore to type in several g++ commands to compile the class and main object files.
I was wondering if there's a way to get a CMD batch file or PowerShell script to just execute the commands one after the other?
Something like this:
g++ -c Area.cpp -o Area.o
g++ -c Convert.cpp -o Convert.o
g++ -c Calculate.cpp -o Calculate.o
g++ -c multi_menu_functions.cpp -o multi_menu_functions.o
g++ -c main.cpp -o main.o
g++ -Wall main.o Area.o Calculate.o Convert.o multi_menu_functions.o -o main
...Something dead simple and easy.
Just write the commands in a file with extension .bat and you can just start that file. You can turn off outputting the commands while execution of the batch file by starting the file with the line #echo off.
Or better yet: Just get make for windows and use that one.
I figured out the issue of why the g++ commands wouldn't work as is: somehow the laptop I was using didn't grant me the correct permissions. At a guess I tried the full path name for g++.exe and it worked. I reconfigured some things and now it works with the commands as listed.
On a side note; I did get gnumake and minGW make working as well. Since these can run my Linux makefiles I'll use these as well.

How can I specify include and library locations for mingw32-make under msys2?

I have a suite of Windows programs that up to now I have built under msys, one of which uses libxml2. I am currently trying to switch to building them under msys2 and the latter one is hitting a problem. My updated Makefile includes this:
CFLAGS += -I/mingw32/include/libxml2
$(BINDIR)/%.o: %.c
#-$(MKDIR) $(BINDIR)
$(CC) $(CFLAGS) -c $*.c -o $#
But when I run make (mingw32-make) the compile fails as follows:
[csv-gen]: mingw32-make
gcc -std=c99 -Werror -Wall -D_XOPEN_SOURCE -O -I/mingw32/include/libxml2 -c xmlParse.c -o ../../bin/Win32/csv-gen/xmlParse.o
xmlParse.c:36:27: fatal error: libxml/parser.h: No such file or directory
#include "libxml/parser.h"
^
compilation terminated.
Makefile:66: recipe for target '../../bin/Win32/csv-gen/xmlParse.o' failed
mingw32-make: *** [../../bin/Win32/csv-gen/xmlParse.o] Error 1
Yet that libxml/parser.h file does exist under the /mingw32/include/libxml2 path given by the -I option:
[csv-gen]: ls /mingw32/include/libxml2/libxml/parser.h
/mingw32/include/libxml2/libxml/parser.h
And the strange thing is that if I run the exact same gcc command directly from the msys bash shell (copying and pasting it from the make output above) then it compiles fine with no errors.
And I have the exact same problem with the link phase where it doesn't find the libxml2.a library in the path given to gcc by -L/mingw32/lib, and again I run the gcc link command directly from the shell and it works fine.
So why would gcc's -I/mingw32/include/libxml2 and -L/mingw32/lib options not work when run via mingw32-make yet the exact same options work fine on calling gcc directly from the shell?
I did try making the paths explicit Windows paths (d:/msys64/mingw32/...) and also tried quoting them, to no avail.
It turned out that the answer was to use explicit Windows paths, which as I said above I had tried, but when I tried again it worked so I must have previously made some mistake doing that. I found this by using the output from pkg-config which gives:
[csv-gen]: pkg-config --cflags libxml-2.0
-ID:/msys64/mingw32/include/libxml2
So apparently the problem is that mingw32-make doesn't fully understand the msys filing system, or at least not /mingw32 and /mingw64 paths in it.

gccfilter and gcc 4.7.2 does not work, stops compiling

I have successfully installed the gccfilter (http://www.mixtion.org/gccfilter/) in my toolchain. The filter should actually work because all perl modules and other stuff has been installed the problem is that it does not work properly when I use for example the following command line:
gccfilter -c -a g++ -std=c++11 -O3 -DNDEBUG -I/"tonnes of includes" -o CMakeFiles/...../main.cpp.o
-c /...path.../App/main.cpp
.../variant.hpp:17:0,
from .../SceneParser.hpp:12,
from .../SimulationManager.hpp:12,
from .../main.cpp:8:
_ <-- Cursor is here
It compiles but after the error message it stops doing anything, the cursor is on the bottom line and nothing happens?
So the tool does not quite work, I am using gcc 4.7.2. i am not quite sure where the problem might be?

Resources