Compiled successfully, but no console output - c

I'm trying to use LLVM on windows.
I've installed llvm-mingw(https://github.com/mstorsjo/llvm-mingw/releases/tag/20220906), and it works like a charm.
Everything works well, but pthread and openmp.
Code compilation done with no errors, so I think there's no linking issue or something like that.
Notice that there's no compilation or linking error below, but no console out.
PS C:\Users\first\Developments\Temp> clang -Wall -Wextra -fopenmp .\test.c
PS C:\Users\first\Developments\Temp> ./a.exe
PS C:\Users\first\Developments\Temp>
Where did I go wrong? Here's my c code and vscode configuration(cmd args).
#include <stdio.h>
#include <omp.h>
int main(void)
{
#pragma omp parallel num_threads(4)
{
printf("%d\n", omp_get_thread_num());
}
return 0;
}
"args": [
"-O0",
"-Wall",
"-Wextra",
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}\\.exe\\${fileBasenameNoExtension}.exe",
"-lm",
"-lpthread",
"-fopenmp"
],

It was becase of so called "Human Error". The computers never lies unless it deal with floating point decimals.
The problem was I did't setup the environment properly.
I did not add path of .dll fils in PATH.
Now that I correct them, it work like a charm!

Related

How can I get VS Code's IntelliSense to behave like GCC with respect to pre-defines?

If I compile and run the following program with gcc, it prints "Defined". However, in VS Code with the C/C++ extension, the line with printf is greyed out, suggesting it won't be executed.
#include <stdio.h>
#include <features.h>
int main(int argc, char *argv[])
{
#ifdef __USE_MISC
printf("%s\n", "Defined");
#endif
}
I tried getting gcc to output its pre-defines with gcc -E -dM main.c, and put all of those into my C_Cpp.default.defines setting, but I get the same behavior.
What is the recommended/most robust way of just getting VS Code to see what gcc sees? The above is a minimal example, but it's causing VS Code to fail to pick up type definitions from various system header files even though the code compiles fine.

Some problems when trying to implement C code on Visual Studio Code

I recently started doing some programming with C/C++. I was using Dev-C++ and today
i changed to Visual Studio Code because later on i'll need to work in other programming languages too, like Python, R etc. I successfully installed it and i added the extensions of C/C++ and Code runner. Although am having some problems when i try to run the following two basic programs:
#include <stdio.h>
int main(void){
printf("hello world\n");
return 0;
}
and
#include <stdio.h>
int main(void){
int x;
printf("Give a number: ");
scanf("%d",&x);
printf("Your number is: %d\n",x);
return 0;
}
In the first program i get the following output in the integrated terminal of VS code:
My first question is a matter of appearance, how can i remove the path that shows above the "hello world" output? Also, notice that i am on the terminal option, if i switch to the output option, nothing is being displayed there, although i have seen in some tutorials that they can display their results in the output option which is more minimalistic than the terminal, in addition it displays the run time of the program which is a nice feature.
In the second program when i press the Run Code button it opens my cmd first and asking to give a number, after i give the number and press Enter i can type the asked number also in the integrated terminal of VS and get the result. Is there anyway to surpass my cmd? I.e. to run the program only in VS without running it also through the cmd? Note that i have already checked the option "Run In Terminal" in the settings of the code runner extension.
Thanks in advance!
So I believe you are on windows, simply installing the extension wont help, there are a few more steps to this.
You'll create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable file based on the source code.
From the main menu, choose Terminal > Configure Default Build Task. In the dropdown, which will display a tasks dropdown listing various predefined build tasks for C++ compilers. Choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.
This will create a tasks.json file in a .vscode folder and open it in the editor.
Your new tasks.json file should look similar to the JSON below:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
To run the build task defined in tasks.json, press Ctrl+Shift+B or from the Terminal main menu choose Run Build Task.
You can run your program in the terminal by typing .exe (or .<your program name.exe if you use a PowerShell terminal).
Hello you should try to install the Code Runner extension. You will run your future C and Python code easily.

How to use runtime tracing in OpenMP LLVM?

I want to enable runtime tracing and see the output. Something like the output of
KD_TRACE(10, ( buff, gtid, schedule, chunk, lb, ub, st ) );
in kmp_dispatch.cpp
Refer this
https://elixir.bootlin.com/llvm/latest/source/openmp/runtime/src/kmp_dispatch.cpp#L624
So, far I have followed the following tutorial:
https://passlab.github.io/CSE436536/Assignments/project_dev_setup.html
But I am not able to see any output from the tracer.
Is there a particular file or something where the output is logged? Or it is logged in the terminal?
I am compiling the openMP program like this:
clang omp1.c -L/PATH/llvm_work/openmp/BUILD/runtime/src -o omp1
ldd omp1
This is the output:
linux-vdso.so.1 (0x00007ffdae305000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fca2f3bb000)
/lib64/ld-linux-x86-64.so.2 (0x00007fca2f7ac000)
I hope this is using the OpenMP I have build from source and not libomp.
omp1.c:
#include<stdio.h>
#include "omp.h"
int main()
{
int i=0;
#pragma omp parallel for schedule(static)
for(i=0;i<1000;++i)
{
int x = 4+i;
}
}
But when I am trying to run this program using the same command I am getting an error.
/tmp/omp2-d969a9.o: In function `main':
omp2.c:(.text+0x1c8): undefined reference to omp_set_num_threads
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone help me with correctly compiling the openMP programs with the openMP code I have built from source and also in using tracer?
Thank you.
You need to compile with -fopenmp flag. Also, you need to have the debug version of the runtime (built with debug info) + set the environment variable export KMP_DEBUG=511.
I think you have to tell the compiler, that you want to use OpenMP via -fopenmp:
clang -fopenmp omp1.c -L/PATH/llvm_work/openmp/BUILD/runtime/src -o omp1

how to use OpenMP in CodeLite?

I want to use OpenMP in CodeLite, but it doesn't work.
I have already choose -fopenmp in compile setting(actually it's the default setting).
My program is as follows:
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel
{
printf("Hello World!");
}
return 0;
}
The result of this program shows that "#include " doesn't work, and the "#pragma omp parallel" is ignored.
Make sure the openmp support library is installed, for example, on Linux with gcc, you should install the libgomp package, for example if you are on fedora, run dnf install libgomp, then try compile again.
It is necessary to pass -fopenmp to the linker to enable OpenMP features. The following steps should help you achieve that:
Right click on the project in the workspace view.
Select "Settings" near the bottom of this pop-up menu. Settings...->Linker->Linker Options.
Click into the semicolon delimited list to reveal ellipses and click on the ellipses.
Click the checkbox for "Enable OpenMP (linkage) [-fopenmp]"
Re-compile the code - It should work!

How to run Pthreads on windows

I used to use a mac to write some C programs but it's not working now.
I have to use an old windows laptop for a while.
I installed codeblocks and tested a simple program using Pthreads. Unfortunately it didn't work.
pthread_create(&thrd1, NULL, thread_execute, (void *)t);
It keeps saying undefined reference to _imp__pthread_create
How can i fix it?
You've clearly got a version of pthreads for Windows. You just haven't included the .lib file in your linker settings. Do that and you should be golden.
You need to grab pthreads-win32 as pthreads is a Unix component not a Windows one.
If you are using MinGW you can MinGW installation manager and install packages that need to execute pthreads and openmp related tasks. Here is the procedure.
After opening the installation manager go to all packages and select the select packages named using mingw32-pthreads-w32 and select them for installation.
Then go to the installation -> Apply changes to install new packages. The you can use pthread.h and omp.h inside your c or c++ program without any problem.
This code works fine in an MSYS2 terminal on Windows.
All you need to do is to install gcc. (See further below.)
// hello.c
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *print_hello(void *thrd_nr) {
printf("Hello World. - It's me, thread #%ld\n", (long)thrd_nr);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
printf(" Hello C code!\n");
const int NR_THRDS = omp_get_max_threads();
pthread_t threads[NR_THRDS];
for(int t=0;t<NR_THRDS;t++) {
printf("In main: creating thread %d\n", t);
pthread_create(&threads[t], NULL, print_hello, (void *)(long)t);
}
for(int t=0;t<NR_THRDS;t++) {
pthread_join(threads[t], NULL);
}
printf("After join: I am always last. Byebye!\n");
return EXIT_SUCCESS;
}
Compile and run as follows:
gcc -fopenmp -pthread hello.c && ./a.out # Linux
gcc -fopenmp -pthread hello.c && ./a.exe # MSYS2, Windows
As you can see, the only difference between Linux and MSYS2 on Windows
is the name of the executable binary. Everything else is identical.
I tend to think of MSYS2 as an emulated (Arch-)Linux terminal on
Windows.
To install gcc in MSYS2:
yes | pacman -Syu gcc
Expect output similar to:
Hello C code!
In main: creating thread 0
Hello World. - It's me, thread #0
In main: creating thread 1
Hello World. - It's me, thread #1
After join: I am always last. Bye-bye!
Reference
https://www.msys2.org/wiki/MSYS2-installation/

Resources