0

I am trying to compile my project for the university, everything worked fine until I had to use the sin() function. I get the error:

/home/patrick/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/181.4668.70/bin/cmake/bin/cmake --build /home/patrick/CLionProjects/Aufgabe4_TEI2/cmake-build-debug --target TEI2_A3 -- -j 2
[ 33%] Linking C executable TEI2_A3
CMakeFiles/TEI2_A3.dir/resources/wave.c.o: In Funktion »berechneSignalWert«:
/home/patrick/CLionProjects/Aufgabe4_TEI2/resources/wave.c:38: Warnung: undefinierter Verweis auf »sin«
collect2: error: ld returned 1 exit status
CMakeFiles/TEI2_A3.dir/build.make:120: recipe for target 'TEI2_A3' failed
make[3]: *** [TEI2_A3] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/TEI2_A3.dir/all' failed
make[2]: *** [CMakeFiles/TEI2_A3.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/TEI2_A3.dir/rule' failed
make[1]: *** [CMakeFiles/TEI2_A3.dir/rule] Error 2
Makefile:118: recipe for target 'TEI2_A3' failed
make: *** [TEI2_A3] Error 2

The project is a Cmake-Project, I have tried the answers out of this post: CLion CMakeLists.txt add argv arguments to configuration but they did not work. Below my CMakeLists.txt

cmake_minimum_required(VERSION 3.9)
project(TEI2_A3 C)

set(CMAKE_C_STANDARD 11)

set(CMAKE_C_FLAGS "-lm")

include_directories(.)
include_directories(./../Aufgabe4_TEI2)
add_executable(TEI2_A3
        test.wav
        main.c resources/wave.h resources/wave.c)

You can one of my tries still in there. I know the problem about the , but I couldn't solve it yet. Does anyone have an idea how to do this in CLion?

My file looks like this

#define _CRT_SECURE_NO_WARNINGS
#define M_PI 3.1415926535897932384626433832795028841971

#include "wave.h"
#include <stdio.h>
#include <string.h>
#include <math.h>

void writePCM(char * name, float *signal, int N, wavheader header)
{
    FILE *fileOut = fopen(name,"wb");
    chunkheader dataheader;

    header.riff_chunk_header.chunk_size =
            /*Größe chunks RIFF+fmt */ sizeof(header)/*36*/ - sizeof(chunkheader) /*8*/ /*subchunk header RIFF zählt nicht mit*/ +
                                       /*Größe subchunk data*/ sizeof(chunkheader) /*8*/ + N*4 ;
    fwrite(&header,sizeof(header),1,fileOut);

    strncpy(dataheader.chunk_id,"data",4);
    dataheader.chunk_size=N*4;
    fwrite(&dataheader,sizeof(dataheader),1,fileOut);
    fwrite(signal,4,N,fileOut);

    fclose(fileOut);
}
//Mittelwert der Sample_Rate: 6595.007324
float mittelWert(float* array){
    float sum = 0;
    printf("Number of entries: %f\n", array[0]);
    for (int j = 1; j < array[0]; ++j) {
        sum += fabs(array[j] - j-1);
    }
    return sum/array[0];
}


float berechneSignalWert(float n, float f, float a, float r){
    return (float) sin(f * 2 * M_PI * (n / r));
}

float* sinusSignal(float* N, float frequenz, float amplitude, float Abtastrate){
    float* array;
    for (int n = 1; n <= N[0]; ++n) {
        array[n] = berechneSignalWert(n, frequenz, amplitude, Abtastrate);
    }
    return array;
}

The function berechneSignalWert returns the error on the sin() call

Patrick.H
  • 535
  • 3
  • 6
  • 21
  • Please *copy-paste the error message verbatim*. I am pretty sure the error doesn't misspell *undefined* but I cannot check the actual error message on your computer. People using the Google search might not find your question with a misspelt error message. – Antti Haapala -- Слава Україні May 02 '18 at 08:54
  • @AnttiHaapala sorry! Thought it might be better than a german Error description – Patrick.H May 02 '18 at 09:04
  • Possible duplicate of [How to add "-l" (ell) compiler flag in CMake](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) – Tsyvarev May 02 '18 at 11:13
  • @Tsyvarev it is not a duplicate since he has another Problem and linking does not work because it is not selfmade – Patrick.H May 02 '18 at 12:51
  • You got linking problem, which you have tried to resolve by setting `CMAKE_C_FLAGS` variable. Referenced question explains, why this approach is wrong, and provides correct approach. – Tsyvarev May 02 '18 at 13:42
  • @Tsyvarev yes I have tried exactly what was written there but probably I am too dumb, I know it is a linking problem but I cant solve it – Patrick.H May 02 '18 at 13:48
  • Have you tried `target_link_libraries(TEI2_A3 m)`, as my answer to the referenced question suggests? – Tsyvarev May 02 '18 at 13:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170225/discussion-between-patrick-h-and-tsyvarev). – Patrick.H May 02 '18 at 13:51
  • 1
    Well, more suitable duplicate question would be: https://stackoverflow.com/questions/34625627/adding-math-library-with-cmake. – Tsyvarev May 02 '18 at 13:58
  • 1
    You can also produce the English error with `LC_MESSAGES=C cmake` - perhaps if you can provide the 2, then even better! – Antti Haapala -- Слава Україні May 02 '18 at 14:15

0 Answers0