0
votes

I know there are huge amount of posts, but going through lots of them i found nothing that worked.

I want to include some .h and .c files into my C++ file.

Clicking into the method in CLion it redirects me to that foo.h file, but in the end it's not working with following message:

Undefined symbols for architecture x86_64: "_fooFct", referenced from: _main in main.cpp.o ld: symbol(s) not found for architecture x86_64

foo.h

 void fooFct();

foo.c

void fooFct(){
    /* do some stuff here */
 }

main.cpp

#include <iostream>

extern "C"
{
    #include "clibraryFolder/header/foo.h"
}

int main() {
    fooFct();
    return 0;
}

CMakeLists.txt

 cmake_minimum_required(VERSION 3.6)
 project(newcsample)

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

 set(SOURCE_FILES main.cpp)
 add_executable(newcsample ${SOURCE_FILES})

But I don't want to include the C files in the CMakeFiles.txt. Is there another way doing this than by editing the CMakeFiles?

1
What does your CMakeLists.txt file look like? Does it list foo.c in the source list?Some programmer dude
you are declaring the fooFct() function, but you are not linking in its definition: the latter should be in the object file created for foo.c, are you linking it as well?Luca Cappa
you mean something like gcc -c Cfile.c, gcc -shared -o libCfile.so cFile.o, c++ -L/somehwere -Wall main.cpp -o main -lCfile? Nope, I wanted to fully get it done by CLion. Or isn't it possible?flyOWX

1 Answers

4
votes

make the following changes in your CMakeLists.txt file

 cmake_minimum_required(VERSION 3.6)
 project(newcsample)

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

 set(SOURCE_FILES main.cpp foo.c) #all .cpp files and .c files here
 add_executable(newcsample ${SOURCE_FILES})

and if the #include you can specify only the current directory if the .h file is there.