C Code linking to C++ in CLion not working - c++

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?

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.

Related

Crosscompiling cpp Shared Object results in "Undefined Symbol" but works for c Setup

I want to crosscompile a shared library (c and cpp code) on my Ubuntu System (x86_64) for Android aarch64. For reasons of simplicity for this example I created only a source and a header file.
The header file header.hpp within /include
int test();
The source file src.cpp within /src
#include "../include/header.hpp" // #include "../include/header.h"
int test() {
return 42;
}
My CMakeLists.txt
cmake_minimum_required(VERSION 3.6.0)
set(CMAKE_TOOLCHAIN_FILE android.toolchain.cmake)
project(testlibrary)
# find header & source
file(GLOB_RECURSE SOURCE_CPP "src/*.cpp")#file(GLOB_RECURSE SOURCE_C "src/*.c")
file(GLOB_RECURSE HEADER "include/*.h")
add_library(${PROJECT_NAME} SHARED
${SOURCE_CPP}
${HEADER}
)
My android.toolchain.cmake
cmake_minimum_required(VERSION 3.6.0)
set(CMAKE_CXX_COMPILER "/usr/bin/aarch64-linux-gnu-g++")#set(CMAKE_C_COMPILER "/usr/bin/aarch64-linux-gnu-gcc")
set(CMAKE_ANDROID_NDK /home/ubuntu/Android/Sdk/ndk/21.4.7075529)
After calling my function within my app I receive the following error message:
Could not invoke FunctionCaller.test_function
null
Error looking up function 'testFunc': undefined symbol: testFunc
Not finding testFunc seems to be related to not compiling or correctly linking my .cpp/.hpp files I suppose.
Changing my setup from cpp to c and everything is found and I receive the correct return value.
C++
C
src.cpp
src.c
header.hpp
header.c
#include "../include/header.hpp"
#include "../include/header.h"
file(GLOB_RECURSE SOURCE_CPP "src/*.cpp")
file(GLOB_RECURSE SOURCE_C "src/*.c")
set(CMAKE_CXX_COMPILER "/usr/bin/aarch64-linux-gnu-g++")
set(CMAKE_C_COMPILER "/usr/bin/aarch64-linux-gnu-gcc")
What am I missing here? I expected it to fail for both configurations since I installed both compilers the same way.

EXE doesn't work correctly with DLL in CMake [duplicate]

This question already has answers here:
How do I make CMake output into a 'bin' dir?
(9 answers)
Closed 2 years ago.
I've read a number of posts concerning similar issues but I still can't find the solution to the following problem.
I have two CLion (OS Windows) projects mylib and myexe which reside in separate directories at the same level. mylib consists of two files:
library.h
void hello();
and library.cpp
#include "library.h"
#include <iostream>
void hello() {
std::cout << "Hello, World!" << std::endl;
}
The CMakeLists.txt for mylib is the following:
cmake_minimum_required(VERSION 3.16)
project(mylib)
set(CMAKE_CXX_STANDARD 14)
add_library(mylib SHARED library.cpp library.h)
Next, the project myexe consists of one file main.cpp
#include "../mylib/library.h"
int main() {
hello();
return 0;
}
with the following CMakeLists.txt file
cmake_minimum_required(VERSION 3.16)
project(myexe)
set(CMAKE_CXX_STANDARD 14)
add_executable(myexe main.cpp)
find_library(RESULT mylib "d:/src/test/mylib/cmake-build-debug")
target_link_libraries(myexe "${RESULT}")
Both projects are built without errors. But when I run myexe, the "Hello, world" isn't printed, and I'm getting the following:
Process finished with exit code -1073741515 (0xC0000135)
Please help me, how to solve this issue and link DLL correctly.
As #Scheff suggested, looking at How do I make CMake output into a 'bin' dir?, I just added theses three lines to the CMakeLists.txt files of both projects myexe and mylib:
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "../../bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "../../bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../../bin")
where "../../bin" is the desired directory to store .dll and .exe.

Error linking against boost python

Here's my simple HelloWorld program
#include <boost/python.hpp>
using namespace boost::python;
void greet() {
// do nothing
}
BOOST_PYTHON_MODULE(HelloWorld)
{
def("greet", greet);
}
and here's my CMakeLists.txt file
cmake_minimum_required(VERSION 2.8.4)
project(HW)
find_package(Boost COMPONENTS python3 REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${Boost_INCLUDE_DIRS} /Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m include)
file(GLOB_RECURSE SRC
HelloWorld.cpp
)
add_library(HelloWorld SHARED ${SRC})
target_link_libraries(HelloWorld ${Boost_LIBRARIES})
However, I have been unable to build this simple program, with this build error
Undefined symbols for architecture x86_64:
"__Py_NoneStruct", referenced from:
boost::python::detail::none() in HelloWorld.cpp.o
boost::python::api::object::object() in HelloWorld.cpp.o
"boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:
_PyInit_HelloWorld in HelloWorld.cpp.o
ld: symbol(s) not found for architecture x86_64
What am I missing? Sorry if this looks like a newbie question but I'm actually stuck.
I think you're missing the a link to the Python library (as opposed to the Boost Python library)
Try something like find_package(Python) then target_link_libraries(HelloWorld ${Python_LIBRARY})
Additionally (based on this post https://www.preney.ca/paul/archives/107) the name of the library you're building doesn't match the name given in BOOST_PYTHON_MODULE. Change it to BOOST_PYTHON_MODULE(libHelloWorld) because cmake implicitly adds a lib to the module name.

Undefined symbols when actually including. Broken compiler/IDE?

I'm testing the CLion IDE, and I'm attempting to write a minimal C++ program. Here's my code:
in main.cpp:
#include "classings.h"
int main() {
classings s;
s.doSomething();
return 0;
}
in classings.h:
class classings {
public:
void doSomething();
};
in classings.cpp:
#include <string>
#include <iostream>
#include "classings.h"
void classings::doSomething() {
std::cout << "hei" << std::endl;
}
I have no clue why this gives me this error:
Undefined symbols for architecture x86_64:
"classings::doSomething()", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm on OSX 10.10.
I think that your source files classings.h and classings.cpp are not included within your CMakeLists.txt.
If you open CMakeLists.txt, it should look something like this:
Incomplete CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(untitled)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp) # your other source files aren't listed
add_executable(my-program ${SOURCE_FILES})
You can fix the problem by including the new source files
Correct CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(untitled)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp classings.hpp classings.cpp) # manually listing all sources
add_executable(my-program ${SOURCE_FILES})
FYI, you can save yourself the hassle of editing the CMakeLists.txt every time you add/remove a new source file by using the file() or aux_source_directory() command. Here's an example:
set(SOURCE_DIRECTORY "src")
file(GLOB_RECURSE SOURCE_FILES "${SOURCE_DIRECTORY}/*.c" "${SOURCE_DIRECTORY}/*.h"
"${SOURCE_DIRECTORY}/*.cc" "${SOURCE_DIRECTORY}/*.hh"
"${SOURCE_DIRECTORY}/*.cpp" "${SOURCE_DIRECTORY}/*.hpp"
"${SOURCE_DIRECTORY}/*.cxx" "${SOURCE_DIRECTORY}/*.hxx")
add_executable(my-program ${SOURCE_FILES})
Or
aux_source_directory("src" SOURCE_FILES)
add_executable(my-program ${SOURCE_FILES})

cmake simple sub-directory not including files

I have read and tried just about every tutorial/wiki/SO post, page, snippet I could find to get this CMAKE working....
I have a super simple directory structure:
ROOT/
|- CMakeLists.txt
|- main.cpp
|- sub/
|-CMakeLists.txt
|-subx/
|-CMakeLists.txt
|-subx.h
|-subx.cpp
|-suby/
|-CMakeLists.txt
|-suby.h
|-suby.cpp
The main.cpp is a super simple cpp program:
//omitting all unnecessary code
int main() {
subx s;
s.defined_method();
s.another_defined_method(1);
return 0;
}
You can assume, for everyone's sake that the subx and suby definitions are correct and work just fine, because they do when I compile by hand.
When I compile by CMake I get the following error:
"/path/to/cmake" --build /path/to/Debug --target CS220_Project -- -j 4
Linking CXX executable simple_project
Undefined symbols for architecture x86_64:
"subx::defined_method()", referenced from:
_main in main.cpp.o
"subx::another_defined_method(int)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [simple_project] Error 1
make[2]: *** [CMakeFiles/simple_project.dir/all] Error 2
make[1]: *** [CMakeFiles/simple_project.dir/rule] Error 2
make: *** [simple_project] Error 2
The root CMakeLists.txt file looks:
cmake_minimum_required(VERSION 2.8.4)
project(simple_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_subdirectory(sub)
add_executable(simple_project ${SOURCE_FILES})
Sub CMakeLists.txt file looks:
add_subdirectory(subx)
add_subdirectory(suby)
subx & suby CMakeLists.txt file looks: (they include their respective distinction)
set(SUBX_SOURCES subx.cpp)
#Add any files in this directory
add_executable(SUBX ${SUBX_SOURCES})
I've tried things like add_library, file (glob), etc. I cannot, for the life of me get files that are in any sub-directory to compile with the main.cpp program.
Depends on what exactly you want the subprojects to be. The way I understand it, subx and suby are libraries, which should be linked to the main executable:
ROOT/CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(simple_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_subdirectory(sub)
add_executable(simple_project ${SOURCE_FILES})
target_link_libraries(simple_project SUBX SUBY)
ROOT/subx/CMakeLists.txt
set(SUBX_SOURCES subx.cpp)
#Add any files in this directory
add_library(SUBX ${SUBX_SOURCES})
(dtto for suby)