How to add linker flags in CLion [duplicate] - c

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
Closed 3 years ago.
Im trying to use libsodium, ive installed the librarys and trying to make the simplests of tests, when making the project using Makefile there are no probkems but when using CLion and Cmake i get "undefined reference to `sodium_init'"
Im working on Ubuntu 18.04.
I found the reason for the error was the need to link to add the -lsodium flag to the linking (adding the flag makes a normal Makefile work)
as i searched the web for ways to link to an external library using CMake or in CLion i found many solutions that did not work, including :
add_link_options(-lsodium)
set (CMAKE_STATIC_LINKER_FLAGS "-lsodium")
this is the code im trying to compile:
#include <stdio.h>
#include <sodium.h>
int main()
{
if (sodium_init() < 0)
{
printf("sodium init error");
}
__uint32_t rand = randombytes_random();
printf("hello %d, this is a random number \n", &rand);
return 0;
}
The full error message in CLion looks like this :
MakeFiles/TestC.dir/main.c.o: In function `main':
/home/--username--/Projects/TestC/main.c:7: undefined reference to `sodium_init'
collect2: error: ld returned 1 exit status
CMakeFiles/TestC.dir/build.make:83: recipe for target 'TestC' failed
make[3]: *** [TestC] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/TestC.dir/all' failed
make[2]: *** [CMakeFiles/TestC.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/TestC.dir/rule' failed
make[1]: *** [CMakeFiles/TestC.dir/rule] Error 2
Makefile:118: recipe for target 'TestC' failed
make: *** [TestC] Error 2
EDIT:
Ive solved the problem, after looking at - https://stackoverflow.com/a/43136754/7276240
I added the following line to my CMakeLists.txt:
target_link_libraries(TestC libsodium.a)
TestC being the name of the executable.

You can use the usual find_package() or find_library() CMake commands and link it with target_link_libraries(). If you don't want to specify the path manually, there already is a Findsodium.cmake in the official repository. Here is one random example of a CMakeLists.txt found on Github.

Related

Setting up the `regex.h` library on Windows with VS Code

I've been trying to get the regex.h library to work all day, but it's not working. (Here are my VS Code workspace configurations: https://github.com/EnderCommunity/Murmur/tree/main/.vscode - all the compiler info are here)
This is the file that I was trying to get the regex library to work with:
https://github.com/EnderCommunity/Murmur/blob/main/compiler/libraries/regex/reg.h
I tried to include the library normally, but it returns an error:
And I tried to include this library, but it returns some compiling errors:
> Executing task: C/C++: gcc.exe build active file <
Starting build...
"C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gcc.exe" -g C:\Users\adels\Desktop\EnderCommunity\Projects\Murmur\compiler\start.c -o C:\Users\adels\Desktop\EnderCommunity\Projects\Murmur\compiler\start.exe
C:\Users\adels\AppData\Local\Temp\ccifSq1r.o: In function `regChk':
C:/Users/adels/Desktop/EnderCommunity/Projects/Murmur/compiler/libraries/regex/reg.h:15: undefined reference to `__imp_regcomp'
C:/Users/adels/Desktop/EnderCommunity/Projects/Murmur/compiler/libraries/regex/reg.h:22: undefined reference to `__imp_regexec'
C:/Users/adels/Desktop/EnderCommunity/Projects/Murmur/compiler/libraries/regex/reg.h:33: undefined reference to `__imp_regerror'
C:/Users/adels/Desktop/EnderCommunity/Projects/Murmur/compiler/libraries/regex/reg.h:40: undefined reference to `__imp_regfree'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
The terminal process terminated with exit code: -1.
Terminal will be reused by tasks, press any key to close it.
So the way that I included this library might be wrong! What am I doing wrong here?
Using MSYS2 will fix this, it comes with all the necessary libraries for C.

How to run multiple C codes on Clion?

This is my first question, so I'll try to explain everything about my problem. So, I had downloaded Clion to code my first C programs for my university degree. So, when I create a simple Hello World on C, I can run normally, but if I create a new program or a new hello world, the program fails with the following error on Clion:
> ====================[ Build | MeusCodigosC_2_0 | Debug ]======================== "C:\Program Files\JetBrains\CLion
> 2020.2.1\bin\cmake\win\bin\cmake.exe" --build "C:\Users\salom\CLionProjects\MeusCodigosC 2.0\cmake-build-debug"
> --target MeusCodigosC_2_0 -- -j 12 Scanning dependencies of target MeusCodigosC_2_0 [ 33%] Building C object
> CMakeFiles/MeusCodigosC_2_0.dir/AtividadeProducaoLabU2.c.obj [ 66%]
> Building C object CMakeFiles/MeusCodigosC_2_0.dir/HelloWorld.c.obj
> [100%] Linking C executable MeusCodigosC_2_0.exe
> c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
> CMakeFiles\MeusCodigosC_2_0.dir/objects.a(HelloWorld.c.obj): na
> função "main": C:/Users/salom/CLionProjects/MeusCodigosC
> 2.0/HelloWorld.c:7: múltiplas definições de "main"; CMakeFiles\MeusCodigosC_2_0.dir/objects.a(AtividadeProducaoLabU2.c.obj):C:/Users/salom/CLionProjects/MeusCodigosC
> 2.0/AtividadeProducaoLabU2.c:32: definido primeiro aqui collect2.exe: error: ld returned 1 exit status mingw32-make.exe[3]: ***
> [MeusCodigosC_2_0.exe] Error 1 mingw32-make.exe[2]: ***
> [CMakeFiles/MeusCodigosC_2_0.dir/all] Error 2 mingw32-make.exe[1]: ***
> [CMakeFiles/MeusCodigosC_2_0.dir/rule] Error 2 mingw32-make.exe: ***
> [MeusCodigosC_2_0] Error 2
> CMakeFiles\MeusCodigosC_2_0.dir\build.make:119: recipe for target
> 'MeusCodigosC_2_0.exe' failed CMakeFiles\Makefile2:94: recipe for
> target 'CMakeFiles/MeusCodigosC_2_0.dir/all' failed
> CMakeFiles\Makefile2:101: recipe for target
> 'CMakeFiles/MeusCodigosC_2_0.dir/rule' failed Makefile:137: recipe for
> target 'MeusCodigosC_2_0' failed
I checked my CMakeLists.txt which I read that it's the file that makes your codes run, and the new code was already added to the target, but I still can't run it. The only workaround I had found is to delete the old code that I'm not using, and leave it only the code I want to use in that specific moment, only then I can run my C codes on Clion. Anyone had already face an issue like this? Thank you!
The error message says (if I understand Portuguese correctly) that the compiled files have several main functions defined in them.
main() is the function that runs when your program is executed. Several functions with the same name mean that the compiler doesn't know which one you want to run.
If you are writing several different programs, just keep them in separate folders, or make sure your CMakeLists.txt includes only one of them.
UPD: It looks like CLion supports one project creating multiple executable files. See documentation.
In your case, CMakeFile.txt should probably contain two lines (file names might need to be corrected):
add_executable(hello_world HelloWorld.c)
add_executable(MeusCodigosC_2_0 AtividadeProducaoLabU2.c)
If HelloWorld and Lab2 should share some code, you may extract it to a separate file, e.g. common.c, then update the project to say
add_executable(hello_world HelloWorld.c common.c)
add_executable(MeusCodigosC_2_0 AtividadeProducaoLabU2.c common.c)
I had this problem in clion and i fixed that. I Just opened new project and ta damm it worked.
it seems so easy to fix but my first time to using this so it was hard little bit for me :)

fatal error: sodium.h: No such file or directory

Thank you #stark I was unaware you had to link the directory as part of #include. This part is now working though when I include.
(sodium_init() < 0)
{
printf("Sodium could not be initialized");
return 1;
}
I now receive the error "undefined reference to 'sodium_init'.
I have tried adding -lsodium to the compile command (gcc -g -lsodium file1.c file2.c file1.h -o file1.c.exe) which gives the error.
"cannot find -lsodium collect2.exe: error: ld returned 1 exit status"
Through further searching I believe I need to tell the compiler where to find -lsodium though cannot find out how.
-- below has been solved using #stark advice --
I have been trying to get the sodium library working for the last 3 days. I have followed the instillation instructions here(https://libsodium.gitbook.io/doc/installation) which all appear to have completed successfully, but still receive the error.
I then found information suggesting I have not linked Visual Studio (community edition) to the library location.
I have attempted to follow the instructions here(https://www.learncpp.com/cpp-tutorial/a2-using-libraries-with-visual-studio-2005-express/) but cannot see "VC++ Directories". I have tried several other sets of directions with the same outcome.
Is anyone able to help getting the sodium library working so I can get access to the RtlGenRandom() function?

get_peer_certificate error when using wolfSSL

I'm new to using wolfSSL. I am trying to compile a set of codes using gcc.
gcc -o main main.c -lwolfssl
I encounter an error of main.c:(.text+0x47b): undefined reference to 'wolfSSL_get_peer_certificate'
collect2: error: ld returned 1 exit status upon entering the statement.
A snippet of the code shows the error location:
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
return -1;
}
ret = certverify(CERT_FILE,verifyCert);
WOLFSSL_X509* webCert = wolfSSL_get_peer_certificate(ssl);
I have tried modifying the WOLFSSL_X509* webCert = wolfSSL_get_peer_certificate(ssl); command but it seems to be correct.
I am not too sure why this error is occuring. Can someone please help me with this?
I am using Kali Linux 2019.4 to compile this set of codes.
#wolfSSL_new,
It sounds like the application is failing to link the library so while the right headers are in place to locate the function definitions the final step to link the function is what is failing. Where is libwolfssl.so or libwolfssl.a located on your system? Is it in /usr/local/lib/libwolfssl.so or /usr/local/lib/libwolfssl.a?
(.a is a static library, .so is a shared object library it can be either or)
Once you located where it is try this build command instead (For the sake of an example I am going to assume it is in /usr/local/lib):
gcc main.c -o main -I/usr/local/include -L/usr/local/lib -lwolfssl
Let me know if that resolves the linker error you are seeing.
[UPDATE]
This was resolved by adding the configure setting --enable-opensslextra
[END UPDATE]
Regards,
K

Error compiling the Linux kernel 3.19.0-25-generic

I try to execute fakeroot debian/rules binary-headers binary-genericand compile when building kernel but get the error.
Here error in below
arch/x86/built-in.o:(.rodata+0x1cb8): undefined reference to 'sys_monlak'
make[2]: *** [vmlinux] Error 1
make[2]: Leaving directory '/home/su/linux-lts-vivid-3.19.0/debian/build/build-generic'
make[1]: *** [sub-make] Error 2
make[1]: Leaving directory '/home/su/linux-lts-vivid-3.19.0'
make: *** [/home/su/linux-lts-vivid-3.19.0/debian/stamps/stamp-build-generic] Error 2
I tried to build kernel following Build your own kernel and Clarkson.edu
I don't see any reference to the symbol "sys_monlak" inside any branch of the official Linux kernel. Are you using a branch obtained from somewhere else?
This error would happen if a driver depends on a symbol "sys_monlak" which should be defined elsewhere in the kernel but isn't, or perhaps the correct kernel module isn't enabled in the kernel config.
You can try running:
$ grep -R "sys_monlak"
If this produces a c file then you can then try to find the Makefile which should compile in this c file. If, for example, the file was called "arch/x86/monlak.c" you might find the following in the file "arch/x86/Makefile":
obj-$(CONFIG_MONLAK) += monlak.o
In this case you would want to try to enable CONFIG_MONLAK in the kernel config.
The other possibility is that you might want to disable the module that depends on the symbol 'sys_monlak'. Try to carefully read the description in the Kconfig file to see what each module does (the one that depends and the one that provides).

Resources