CMAKE_CXX_FLAGS are flags for the C++ compiler. -l is a linker flag. To link to a library, use this:
target_link_libraries(alfa_1 m)
You might also want to replace -std=c11 with use of CMAKE_C_STANDARD and related variables (CMAKE_C_STANDARD_REQUIRED, CMAKE_C_EXTENSIONS), and possibly replace use of CMAKE_CXX_FLAGS with a call to target_compile_options().
CMAKE_CXX_FLAGS are flags for the C++ compiler. -l is a linker flag. To link to a library, use this:
target_link_libraries(alfa_1 m)
You might also want to replace -std=c11 with use of CMAKE_C_STANDARD and related variables (CMAKE_C_STANDARD_REQUIRED, CMAKE_C_EXTENSIONS), and possibly replace use of CMAKE_CXX_FLAGS with a call to target_compile_options().
add the link of library in the cmakeList
add_library(math STATIC path/to/file.cpp)
add_executable(cmake_hello main.cpp)
target_link_libraries(cmake_hello math)
and in the class cpp
#include "path/to/file.hpp"
for mode detaills see this link
What Is an Undefined Reference in C Programming Language?
What Types of Data Does Pow() Return?
Hey y'all, as the title says, I'm a wee bit stuck trying to use the math.h library function "pow". Here's my code -
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
//OMITTED CODE
size_t i;
double j;
int k;
for(i = argLen - 1, k = 0; k < argLen; i--, k++) {
j = pow(16, i);
deciVals[k] = deciVals[k] * j;
}
//OMITTED CODE
return 0;
}Despite having math.h included, and using the -lm option for the GCC, I'm still getting this error -
>gcc -lm -o output hexConverter.c /usr/bin/ld: /tmp/ccghjuxO.o: in function `main': hexConverter.c:(.text+0x237): undefined reference to `pow' collect2: error: ld returned 1 exit status
Any help would be appreciated, thanks guys.
Instead of
cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c
Try:
cc -g -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c -lm
When the linker searches a library, it links in modules that contain definitions for previously-undefined symbols.
If the linker searches -lm before foo.o, then pow() is not yet undefined. Conversely, if foo.o comes first, it undefines pow(), which -lm can then resolve.
EDIT: To accomplish this advice in your makefile, make these changes:
CFLAGS=-g -DBLITZ_HOST_IS_LITTLE_ENDIAN
LDLIBS=-lm
...
asm: asm.c
$(CC) $(CFLAGS) asm.c $(LDLIBS) -o asm
I've recently suffer this problem using an automatic builder, namely drone.io.
The problem was that gcc in Ubuntu 12.04 was linking by default with -Wl,--as-needed. In my case, the project was using autotools, which means I was too lazy to write anything to modify the compilation order. Instead, modifying as-needed flag fixed the problem.
-gabriel_LDFLAGS = $(GLIB2_LIBS) $(DBUS_LIBS) -lssh
+gabriel_LDFLAGS = -Wl,--no-as-needed $(GLIB2_LIBS) $(DBUS_LIBS) -lssh
For full info, you can check the fixing commit in https://bitbucket.org/kikeenrique/gabriel/commits/f08eefdca3f7bb90f48f5a6fbfc8839422572508
You can take a look to the log with errors BEFORE applying the fix and also you can take a look to the log without errors AFTER applying the fix.