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})
Related
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.
I try to use the SimpleAmqpClient library to build my multi-agent environment for simulation. I have installed the library after cloning its sources, making them:
make
sudo make install
After that, I created the
main.cpp
file:
#include <iostream>
#include <SimpleAmqpClient/SimpleAmqpClient.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
just to try it out.
Also, I have the following
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
include_directories('/usr/local/include/')
find_package(libSimpleAmqpClient REQUIRED)
include_directories(${libSimpleAmqpClient++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${libSimpleAmqpClient++_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SampleProject ${SOURCE_FILES})
So, the question is: how to find and link this library.
You need to include the line
target_link_libraries(SampleProject ${LIBS})
after the line
add_executable(SampleProject ${SOURCE_FILES})
This imbues the target SampleProject with properties that tell the generator to generate a linker command which reference the libraries you need.
I have found the solution, thanks to #RichardHodges. The solution was to use
find_library() instead of find_package().
THe final file is the following:
CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
include_directories('/usr/local/include/')
find_library(libSimpleAmqpClient REQUIRED)
include_directories(${libSimpleAmqpClient++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${libSimpleAmqpClient++_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SampleProject ${SOURCE_FILES})
I'm basically trying to make an SFML project in CLion which uses CMake.
I first downloaded SFML 2.2 which already had the .dylib files built. It said to install them in usr/local/lib and usr/local/include, but they didn't exist, so I created them and then put the files there.
I then opened up CLion, created a subdirectoy cmake_modules, placed the FindSFML.cmake file inside, and in the root project directory placed these files:
main.cpp
#include <SFML/Graphics.hpp>
int main()
{
sf::err() << "Hello, World!" << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(HelloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(HelloWorld ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML COMPONENTS graphics window system REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(HelloWorld ${SFML_Libraries})
The problem is that when I try to build the project, I get the following error:
Undefined symbols for architecture x86_64:
"sf::err()", 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've tried searching for a solution, but none of them seem to be working. Did I put the files in the wrong place, or did I forget some import CMake setting? I know that setting SFML up with Xcode would be easier, but I'd prefer to use CLion if at all possible.
Ok, so I made an entirely new project, ran xcode-select --install on my terminal to make /usr/local/ a directory that gets searched by the compiler, downloaded SFML 2.3 instead of 2.2, decided to use the FindSFML.cmake file shipped with SFML, and made my CmakeLists.txt file look like so:
cmake_minimum_required(VERSION 3.2)
project(SFMLTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SFMLTest ${SOURCE_FILES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/SFML-2.3/cmake/Modules/")
find_package(SFML REQUIRED graphics window system)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(SFMLTest ${SFML_LIBRARIES})
endif(SFML_FOUND)
This compiled and ran the following code in CLion:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "Window");
while (window.isOpen())
{
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
I don't know what exactly fixed everything, but I'm just glad it did. Hopefully this helps anyone else who's in a similar jam.
For SFML 2.5 and greater a more modern approach has been taken and setting up cmake is easier.
Example CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(SFMLTest)
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
add_executable(SFMLTest main.cpp)
target_link_libraries(SFMLTest sfml-graphics sfml-audio)
Note :
Its no longer needed to setup FindSFML.cmake or SFML_ROOT etc.
SFML_LIBRARIES, SFML_DEPENDENCIES and SFML_INCLUDE_DIR don't exist
anymore
More info https://en.sfml-dev.org/forums/index.php?topic=24070.0
For others who downloaded SFML via brew, I managed to fix this by adding this to my CMakeLists.txt
set(SFML_ROOT /usr/local/lib)
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.
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)