flyOWX
flyOWX

Reputation: 215

C Code linking to C++ in CLion not working

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?

Upvotes: 0

Views: 511

Answers (1)

Seek Addo
Seek Addo

Reputation: 1893

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.

Upvotes: 4

Related Questions