This question already has answers here:
How to link to the C math library with CMake?
(3 answers)
Closed 3 years ago.
To do my homework I need #include "math.h", but after updating GCC and CMake, CLion can't link my project files. What should I do to fix this problem?
In Settings -> Build, Execution and Deployment -> Toolchains CLion says that CMake version is 3.15.3 and GDB version is 8.3 and it's OK.
I already tired to reinstall GCC, CMake and CLion, but it didn't work. Also I tired to search some info on StackOverflow, but still nothing works.
Main.c:
#include <stdio.h>
#include <math.h>
int main() {
FILE *output;
output = fopen("/home/vadimsam/CLionProjects/untitled/data.txt", "w");
double x=0.,v=0.,t=0.,m=0.,k=0.,dt = 1e-5,xn,vn;
while (t < 1e1) {
vn = -x*sqrt((k/m))*cos(sqrt((k/m))*t)+v*cos(sqrt((k/m))*t);
xn = -x*cos(sqrt((k/m))*t)+(v/sqrt((k/m)))*sin(sqrt((k/m))*t);
t += dt; x = xn; v = vn;
fprintf(output, "%lf %lf %lf\n", t, x, v);
}
fclose(output);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(untitled2 C)
set(CMAKE_C_STANDARD 11)
add_executable(untitled2 main.c)
Compiler output:
====================[ Build | untitled2 | Debug ]===============================
/home/vadimsam/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/193.5096.27/bin/cmake/linux/bin/cmake --build /home/vadimsam/CLionProjects/untitled2/cmake-build-debug --target untitled2 -- -j 8
Scanning dependencies of target untitled2
[ 50%] Building C object CMakeFiles/untitled2.dir/main.c.o
[100%] Linking C executable untitled2
CMakeFiles/untitled2.dir/main.c.o: In function `main':
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `cos'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `cos'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `cos'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sin'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled2.dir/build.make:83: recipe for target 'untitled2' failed
make[3]: *** [untitled2] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/untitled2.dir/all' failed
make[2]: *** [CMakeFiles/untitled2.dir/all] Error 2
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/untitled2.dir/rule' failed
make[1]: *** [CMakeFiles/untitled2.dir/rule] Error 2
Makefile:118: recipe for target 'untitled2' failed
make: *** [untitled2] Error 2
I need to compile my project.
The math library is usually linked as a separate library (conveniently named m) that you explicitly need to link with.
You tell CLion (through its CMakeLists.txt file) to link with libraries with the target_link_libraries command:
target_link_libraries(untitled2 m)
Related
I am on Debian 10 and I want to develop an C application for PostgreSQL. This is why I installed a packages postgresql and libpq-dev. Later clearly installed header files inside the system folders:
┌───┐
│ $ │ ziga > ziga--workstation > 001--hello_world
└─┬─┘
└─> find /usr/include/ | grep libpq
/usr/include/postgresql/libpq-fe.h
/usr/include/postgresql/libpq
/usr/include/postgresql/libpq/libpq-fs.h
/usr/include/postgresql/libpq-events.h
/usr/include/postgresql/internal/libpq
/usr/include/postgresql/internal/libpq/pqcomm.h
/usr/include/postgresql/internal/libpq-int.h
But when I compile this simple C program:
#include <stdio.h>
#include <postgresql/libpq-fe.h>
int main(int argc, char * argv[]){
int v = PQlibVersion();
printf("Version of libpq: %d\n", v);
return 0;
}
compiler says:
gcc -std=c17 -Wall -Wpedantic -g -gdwarf-2 -o main.elf main.c
/usr/bin/ld: /tmp/ccVzig6T.o: in function `main':
/home/ziga/Dropbox/workspace/racunalnistvo/projects--pistam/2021-03-03--postgressql_c/001--hello_world/main.c:6: undefined reference to `PQlibVersion'
collect2: error: ld returned 1 exit status
make: *** [makefile:59: main.elf] Error 1
I don't understand. Header files are inside /usr/include where they should be found...
Is library not being found or what? It is also installed...
┌───┐
│ $ │ ziga > ziga--workstation > 001--hello_world
└─┬─┘
└─> find /usr/lib | grep libpq
/usr/lib/x86_64-linux-gnu/libpq.a
/usr/lib/x86_64-linux-gnu/libpq.so
/usr/lib/x86_64-linux-gnu/libpq.so.5
/usr/lib/x86_64-linux-gnu/pkgconfig/libpq.pc
/usr/lib/x86_64-linux-gnu/libpq.so.5.11
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqsignal.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-secure-openssl.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/auth.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqcomm.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-fsstubs.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqformat.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/ifaddr.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-secure.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqmq.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/crypt.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-secure-common.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/hba.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/auth-scram.bc
/usr/lib/postgresql/11/lib/libpqwalreceiver.so
Can anyone tell me how to best solve this?
As the log says, The linker is unable to find any reference to PQlibVersion. Since the linker is running, it clearly is not a problem of compiling but linking.
/usr/bin/ld: /tmp/ccVzig6T.o: in function `main':
/home/ziga/Dropbox/workspace/racunalnistvo/projects--pistam/2021-03-03--postgressql_c/001--hello_world/main.c:6: undefined reference to `PQlibVersion'
collect2: error: ld returned 1 exit status
I think you should add linker options to link against the postgress library. Add -lpq option to you compiler arguments. For a more complete explanation, please read Building libpq Programs.
This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 3 years ago.
When I compile this code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
char str[] = "3.6";
double res = atof(str);
printf("%f\n", sqrt(res));
return 0;
}
I get this error:
====================[ Build | untitled12 | Debug ]==============================
/snap/clion/81/bin/cmake/linux/bin/cmake --build /home/abdo/CLionProjects/untitled12/cmake-build-debug --target untitled12 -- -j 4
[ 50%] Linking C executable untitled12
/usr/bin/ld: CMakeFiles/untitled12.dir/main.c.o: in function `main':
/home/abdo/CLionProjects/untitled12/main.c:11: undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/untitled12.dir/build.make:84: untitled12] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/untitled12.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/untitled12.dir/rule] Error 2
make: *** [Makefile:118: untitled12] Error 2
I get same error when I replace 'sqrt' with 'log' or 'ln' ...
compiler: cc (Ubuntu 8.3.0-6ubuntu1) 8.3.0
The math library must be linked in when compiling. Add the -lm flag to your gcc command, after the source or object file(s).
I installed graphics.h library on my Ubuntu 19.04, and when I write some code in my CLion IDE, it successfully includes the graphics.h Header file, and also after writing some basic functions, the IDE doesn't show any errors, but when I compile the Code it gives Errors.
My Code:
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, NULL);
line(10, 10, 50, 50);
closegraph();
}
My CMakeList.txt File:
cmake_minimum_required(VERSION 3.14)
project(CGR_MP C)
set(CMAKE_C_STANDARD 11)
include_directories("//usr//local//lib//libgraph-1.0.2//")
build_command("lgraph")
add_executable(CGR_MP main.c)
Errors:
====================[ Build | CGR_MP | Debug ]==================================
/snap/clion/73/bin/cmake/linux/bin/cmake --build /home/prathamesh/Desktop/College/Sem-2/CGR-MP/cmake-build-debug --target CGR_MP -- -j 2
Scanning dependencies of target CGR_MP
[ 50%] Building C object CMakeFiles/CGR_MP.dir/main.c.o
[100%] Linking C executable CGR_MP
/usr/bin/ld: CMakeFiles/CGR_MP.dir/main.c.o: in function `main':
/home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:7: undefined reference to `detectgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:8: undefined reference to `initgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:10: undefined reference to `line'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:12: undefined reference to `closegraph'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/CGR_MP.dir/build.make:84: CGR_MP] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/CGR_MP.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/CGR_MP.dir/rule] Error 2
make: *** [Makefile:118: CGR_MP] Error 2
Despite everything being successfully installed, it shows all Graphics functions as undefined reference
I am trying to use a 3rd party library (.so) file in my project. A very simple program to do that:
#include "LibGent.h"
#include "genttypes.h"
int main() {
GT_LIB* gtLib = LibGentGet();
return 0;
}
I compile/link the program above against libgent.so:
gcc testing.c -L/home/username/proj -lgent
I get the following error:
/tmp/ccALOJwU.o: In function `main': testing.c:(.text+0x11): undefined reference to `LibGentGet'
collect2: error: ld returned 1 exit status
However, I can see that the function LibGentGet is defined in the .so file:
readelf -Ws libgent.so | grep "LibGentGet"
301: 0000000000044820 8 FUNC LOCAL DEFAULT 8 LibGentGet
What am I doing wrong?
I am using Cygwin environment with Lua Interpreter package included while cygwin installation.
So I am able to compile and run sample lua progs.
But when i try to execute a sample c file which has lua calls , i am always getting this following error.
$ cc -o ../samples/ctest -Wall ../samples/ctest.c
/tmp/ccOYgLj4.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status
My sample ctest.c file contents:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* lua interpreter */
lua_State* l;
int main () {
int dofile;
/* initialize lua */
l = lua_open();
/* load lua libraries */
luaL_openlibs(l);
/* run the hello.lua script */
dofile = luaL_dofile(l, "hello.lua");
if (dofile == 0) {
/* call foo */
lua_getglobal(l,"foo");
lua_call(l,0,0);
}
else {
printf("Error, unable to run hello.lua\n");
}
/* cleanup Lua */
lua_close(l);
return 0;
}
hello.lua file contents:
print("from c hurray")
on searching the net everywhere they say some linker error and have to include -llua51. So i tried the following .
$ cc -o ../samples/ctest -Wall -llua5.1 ../samples/ctest.c
/tmp/cc3v5Nim.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status
Vedhashree#Vedhashree-PC /cygdrive/c/cygwin/bin
$ ls /usr/lib/liblua*.a
/usr/lib/liblua.a /usr/lib/liblua5.1.a
/usr/lib/liblua.dll.a /usr/lib/liblua5.1.dll.a
Can you help me fix this issue and make my first embedded lua c program work?
Update:
$ cc -o ctesing -Wall ctesting.c -llua5.1
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua5.1
collect2: ld returned 1 exit status
-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua51
collect2: ld returned 1 exit status
-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua51
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llua
collect2: ld returned 1 exit status
-----------------------------------------------------------------
Still I get only these errors :(
Place -llua5.1 after ../samples/ctest.c. Objects should be linked in reverse order of dependency.
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua5.1
UPDATE: Your update describes a different problem. In this case the linker cannot find a liblua5.1.a file in its search path. Make sure that you have such a library on your system and try adding its path using the -L option.