CMake & C++ : linker error : undefined reference to function - c++

I am trying to compile a simple C++ program with CMake, but I am getting a linker error :
[2/2] Linking CXX executable bin/MY_PROGRAM
FAILED: bin/MY_PROGRAM
: && g++ -g CMakeFiles/MY_PROGRAM.dir/src/main.cpp.o -o bin/MY_PROGRAM && :
/usr/bin/ld: CMakeFiles/MY_PROGRAM.dir/src/main.cpp.o: in function `main':
/home/user/Code/root/src/main.cpp:27: undefined reference to `str_toupper(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
I have tried looking at some questions with similar issues but couldn't find what I did wrong. There must be a problem with my directory structure and CMake files. I could change the directory structure to make things easier, but I may be missing something important and I'd like to figure out why. Also, I am new to C++ so I might be doing something wrong in the code itself, but my IDE doesn't find any issue.
My directory structure is :
root
|-- CMakeLists.txt
|-- src
|-- main.cpp
|-- utils
|-- CMakeLists.txt
|-- src
|-- strutils.cpp
|-- include
|-- strutils.h
The top-level CMakeLists.txt is :
// general stuff (standard, etc...)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
set(MY_PROGRAM_SRC src/main.cpp)
add_executable(MY_PROGRAM ${MY_PROGRAM_SRC})
include_directories(utils/include)
link_libraries(MY_LIBRARY)
and utils/CMakeLists.txt :
set(MY_LIBRARY_SRC include/strutils.h src/strutils.cpp)
add_library(MY_LIBRARY STATIC ${MY_LIBRARY_SRC})
// This is to include strutils.h in strutils.cpp
// (but is it needed ? I could just #include <string> in strutils.cpp, I guess)
target_include_directories(MY_LIBRARY PUBLIC include)
Finally, the source files :
// strutils.h
#include <string>
void str_toupper(const std::string &in, std::string &out);
// strutils.cpp
#include <algorithm>
#include <strutils.h>
void str_toupper(const std::string &in, std::string &out) {
std::transform(in.begin(), in.end(), out.begin(), [](char c){return std::toupper(c);});
}
// main.cpp
#include <strutils.h>
#include <cstdlib>
#include <cstdio>
#include <string>
int main(int argc, char *argv[]) {
// ...
std::string arg_in;
for (int i = 0; i < argc; i++) {
str_toupper(std::string(argv[i]), arg_in);
}
}
Does anyone have an idea of what's going on ? Thanks !

The first sentence in the manual link_libraries
Link libraries to all targets added later.
You use this directive after the target MY_PROGRAM is added - the target MY_PROGRAM is added prior link_libraries.
Prefer use target_link_libraries(MY_PROGRAM MY_LIBRARY) - other targets can require different dependencies and it's better not use a global set of dependencies for all targets.

Related

Cannot Configure CLion for Linking C++

I have a simple c++ example that I am trying to compile on a MacOSX machine using the CLion IDE. The goal is to use header files and cmake to compile this code on CLion with an aim to expand to something bigger and so for now, I have simplified my code to have a bar.h header file and a bar.cpp implementation file. I am getting compilation issues during linking. My code looks as follows:
main.cpp
#include "bar.h"
using namespace std;
int main(int argc, char** argv){
cout<<"starting"<<endl;
Bar bar = Bar("Using Bar Function From Main");
bar.foo();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
set(CMAKE_CXX_STANDARD 11)
project(librarytest)
add_executable(myprog main.cpp)
add_subdirectory(bars)
target_link_libraries(myprog PRIVATE bars)
bars\bar.h
#pragma once
#include <string>
#include <iostream>
class Bar{
private:
std::string s;
public:
Bar(std::string s);
void foo();
};
bars\bar.cpp
#include <string>
#include <iostream>
class Bar{
private:
std::string s;
public:
Bar(std::string s){
std::cout<<"Function Bar"<<std::endl;
this->s = s;
}
void foo(){
std::cout << this->s << std::endl;
}
};
bars\CMakeLists.txt
add_library(bars OBJECT
bar.cpp
)
target_include_directories(bars PUBLIC .)
I get the following error message from my CLion CMake build (I have obfuscated by personal details/directory):
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /{obfuscated-for-security}/testingincludes/cmake-build-debug --target all
[3/3] Linking CXX executable myprog
FAILED: myprog
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names bars/CMakeFiles/bars.dir/bar.cpp.o CMakeFiles/myprog.dir/main.cpp.o -o myprog && :
Undefined symbols for architecture x86_64:
"Bar::foo()", referenced from:
_main in main.cpp.o
"Bar::Bar(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", 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)
ninja: build stopped: subcommand failed.
I have searched other Stack Overflow articles on similar errors but none can explain the issue as it pertains to linking on the OSX. I have followed some of the guidance like re-installing the compiler.
Can anyone suggest how to get this working on the MacOSX using CLion? Any pointers or suggestions would be welcomed.
The problem is not the build (AFAIK), it's the code.
Your bar.cpp file should look like this
#include "bar.h"
#include <string>
#include <iostream>
Bar::Bar(std::string s){
std::cout<<"Function Bar"<<std::endl;
this->s = s;
}
void Bar::foo(){
std::cout << this->s << std::endl;
}
Your version duplicates the Bar class definition in both the header and cpp file. My version includes the Bar class definition and just adds the definitions of the constructor and member function.

undefined reference to `boost::archive::archive_exception::..... appears when building with cmake

I have been trying to write a simple program, which simply storage a structure of vector and integer, and read it back later. The code is shown as below:
#include <iostream>
#include <string>
#include <fstream>
#include <queue>
#include <vector>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
struct testS
{
int num1;
vector<int> num2;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & num1;
ar & num2;
}
};
int main()
{
std::queue<std::string> files;
for(int i = 0;i<50;i++){
testS t1 = testS();
t1.num1=i;
for(int k=0;k<10;k++){
t1.num2.push_back(k);
}
std::string fileName = std::to_string(i)+".test";
std::ofstream tempSaveFile(fileName);
boost::archive::text_oarchive oa(tempSaveFile);
oa<<t1;
files.push(fileName);
}
for (int i=0;i<50;i++){
std::string curRecName =files.front();
std::cout<<"file name: "<<curRecName<<std::endl;
std::ifstream input_file(curRecName);
testS curRec;
boost::archive::text_iarchive ia(input_file);
ia>>curRec;
for(int k=0;k<10;k++){
std::cout<<curRec.num2[k];
}
std::cout<<'\n';
std::remove(&curRecName[0]);
files.pop();
}
}
However, the above cannot even compile since there is some problem with my CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(RecorderTest)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -pthread -lboost_serialization" ) #you can set with add_definitions("-Wall -std=c+11, etc")
set(Boost_INCLUDE_DIR /home/lowlimb/Downloads/boost_1_70_0)
set(Boost_LIBRARY_DIR /home/lowlimb/Downloads/boost_1_70_0/stage/lib)
find_package(Boost 1.70.0 COMPONENTS system filesystem REQUIRED)
include_directories(
include
src
${Boost_INCLUDE_DIR}
)
add_library(
recLib
include/Recorder.hpp
include/Recorder.cpp
)
link_directories(${Boost_LIBRARY_DIR})
enable_testing()
add_executable(rec_o src/main.cpp )
target_link_libraries(rec_o PRIVATE recLib ${Boost_LIBRARIES})
The result I get is
CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
Call Stack (most recent call first):
/usr/share/cmake-3.10/Modules/FindBoost.cmake:907 (_Boost_COMPONENT_DEPENDENCIES)
/usr/share/cmake-3.10/Modules/FindBoost.cmake:1558 (_Boost_MISSING_DEPENDENCIES)
CMakeLists.txt:6 (find_package)
CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
Call Stack (most recent call first):
/usr/share/cmake-3.10/Modules/FindBoost.cmake:907 (_Boost_COMPONENT_DEPENDENCIES)
/usr/share/cmake-3.10/Modules/FindBoost.cmake:1558 (_Boost_MISSING_DEPENDENCIES)
CMakeLists.txt:6 (find_package)
-- Boost version: 1.70.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lowlimb/cdrive/UCLA/lab/Exoskeleton/Controller/Test/TestPtr/Final/build
[ 25%] Building CXX object CMakeFiles/recLib.dir/include/Recorder.cpp.o
[ 50%] Linking CXX static library librecLib.a
[ 50%] Built target recLib
[ 75%] Building CXX object CMakeFiles/rec_o.dir/src/main.cpp.o
[100%] Linking CXX executable rec_o
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_oarchive::text_oarchive(std::ostream&, unsigned int)':
main.cpp:(.text._ZN5boost7archive13text_oarchiveC2ERSoj[_ZN5boost7archive13text_oarchiveC5ERSoj]+0x25): undefined reference to `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::ostream&, unsigned int)'
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_iarchive::text_iarchive(std::istream&, unsigned int)':
main.cpp:(.text._ZN5boost7archive13text_iarchiveC2ERSij[_ZN5boost7archive13text_iarchiveC5ERSij]+0x25): undefined reference to `boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::text_iarchive_impl(std::istream&, unsigned int)'
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl()':
main.cpp:(.text._ZN5boost7archive18text_oarchive_implINS0_13text_oarchiveEED2Ev[_ZN5boost7archive18text_oarchive_implINS0_13text_oarchiveEED5Ev]+0x32): undefined reference to `boost::archive::basic_text_oprimitive<std::ostream>::~basic_text_oprimitive()'
CMakeFiles/rec_o.dir/src/main.cpp.o: In function `boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::~text_iarchive_impl()':
This is just part of the output, yet the rest are in similar pattern. I wonder did I made any mistake in CMakeLists.txt? I can assure the paths are 100% correct, no typo there.
Your find_package statement is missing boost::serialization in the required components:
find_package(Boost 1.70.0 COMPONENTS system filesystem serialization REQUIRED)

CMakelists.txt for compiling HDF5?

I am trying to compile a simple program to read a HDF5 file. The code compiles correctly with h5c++. However I need a cmakelists.txt for the same
readdata.cpp
#include <iostream>
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "testfile.h5" );
int main (void)
{
H5File openFile( FILE_NAME, H5F_ACC_RDONLY );
}
I tried a cmakelists for it but it didnt work. It gave "not defined errors"
readdata.cpp:(.text+0x1d): undefined reference to `H5::FileAccPropList::DEFAULT'
readdata.cpp:(.text+0x24): undefined reference to `H5::FileCreatPropList::DEFAULT'
readdata.cpp:(.text+0x38): undefined reference to `H5check_version'
readdata.cpp:(.text+0x54): undefined reference to `H5::H5File::H5File(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
readdata.cpp:(.text+0x60): undefined reference to `H5::H5File::~H5File()'
CMakelists.txt
cmake_minimum_required(VERSION 3.1.0)
PROJECT (readhdf5)
find_package(HDF5 REQUIRED)
include_directories(${HDF5_INCLUDE_DIRS})
add_executable( readdata readdata.cpp )
target_link_libraries( readdata ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})
If i put the HDF5_CXX_LIBRARIES and HDF5_LIBRARIES manually the it works.
target_link_libraries( readdata libhdf5.so libhdf5_cpp.so)
So it is not able to read $HDF5_CXX_LIBRARIES and $HDF5_LIBRARIES.How can I fix this?
The code you attempt to compile depends on the HDF5 C++ bindings, which are not searched for by CMake's HDF5 module by default. Explicitly add the binding to the find_package command:
find_package(HDF5 REQUIRED COMPONENTS C CXX)

"clang: error: linker command failed with exit code 1" on "ld: symbol(s) not found for architecture x86_64"

Right, please bare with on this, it might be quite a long one, and one related issue was solved here (I think): CMake make[2]: *** No rule to make target `/path/to/uthash/utarray.h', needed by `HelloTest'. Stop.
I have been struggling for some days now to build a simple 'Hello World' programme which mixes C and C++, and pulls in various external libraries using CMake. For full disclosure it should be known that I am fairly new to C, C++ and CMake hence please be nice.
I am working in OS X Yosemite 10.10.4 (my googling seems to suggest this might be part of the problem). I am working out of the CLion IDE.
Alas, here we go, here is the programme I am trying to build:
#include <iostream>
#include "Simbody.h" \\Written in C++
extern "C"
{
#include "project_in_C.h"
}
using namespace std;
using namespace SimTK;
int main(int argc, char** argv) {
cout << "Hello, World!" << endl;
return 0;
}
We have a physics library written in C++ a bespoke project written in C, for which the main header file is, lets call it; project_in_C.h.
Again; all I am trying to do is build a simple mixed C/C++ project.
Now, the above is executed using CMake, and the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(HelloTest)
# Simbody
find_package(Simbody REQUIRED)
include_directories(${Simbody_INCLUDE_DIR})
link_directories(${Simbody_LIB_DIR})
# Project C Headers
set(PROJC_ROOT_DIR "/Users/usr/project-c")
set(PROJC_INCLUDE_DIR ${PROJC_ROOT_DIR}/src
${PROJC_ROOT_DIR}/dir1
${PROJC_ROOT_DIR}/dir2)
include_directories(${PROJC_INCLUDE_DIR})
# Check that it has found the most important header
find_path(projFound project_in_C.h PATHS "/Users/usr/project-c/src")
if(NOT projFound)
message(FATAL_ERROR "Cannot find folder containing project_in_C.h")
endif()
# Project C Source [we want to avoid globbing]
set(PROJC_SOURCE ${PROJC_ROOT_DIR}/src/file1.c
${PROJC_ROOT_DIR}/src/file2.c
${PROJC_ROOT_DIR}/src/file3.c
${PROJC_ROOT_DIR}/src/file4.c
${PROJC_ROOT_DIR}/dir1/file5.c)
# Make library from source files
add_library(PROJC_LIBRARIES ${PROJC_SOURCE})
# Tie together
set(SOURCE_FILES main.cpp)
add_executable(HelloTest ${SOURCE_FILES})
target_link_libraries(HelloTest ${Simbody_LIBRARIES} ${PROJC_LIBRARIES})
So far so good, but here is where the truly mysterious problem arises. Upon build this is what I get in return:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/usr/Library/Caches/clion10/cmake/generated/c1d0f54d/c1d0f54d/Debug --target all -- -j 2
Scanning dependencies of target HelloTest
[ 91%] Built target PROJC_LIBRARIES
[100%] Building CXX object CMakeFiles/HelloTest.dir/main.cpp.o
Linking CXX executable HelloTest
Undefined symbols for architecture x86_64:
"_program_execution_wrapper", 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[2]: *** [HelloTest] Error 1
make[1]: *** [CMakeFiles/HelloTest.dir/all] Error 2
make: *** [all] Error 2
But what the flippin' dynamite does that actually mean?
It is kicking up a fuss with project_in_C.h where the section the error seems to be referring to is written as so:
int program_execution_wrapper(int argc, char **argv);
int __program(int argc, char **argv);
#define main main(int argc, char **argv) { return program_execution_wrapper(argc, argv); } int __program
As ever, any help greatly appreciated.
Em...Undefined symbols is _program_execution_wrapper ... I don't see this symbol anywhere in you program. Is this maybe typo in your post? what if you change code in project_in_C.h from prog_exec_wrapper to program_execution_wrapper? And also, this function is not implemented in your code. For a start try something like this:
int program_execution_wrapper(int argc, char **argv) { printf("%s", "something"); }
Edit based on your comments:
See the following example:
We have two files (main.cpp)
#include <iostream>
int sum(int a, int b);
int main()
{
int a = sum(3, 5);
std::cout << a << std::endl;
return 0;
}
and file sum.cpp
#include <iostream>
int sum(int a, int b)
{
return a+b;
}
So I've I would like to compile this program...it would be like this
g++ main.cpp
I get the following error:
Undefined symbols for architecture x86_64:
"sum(int, int)", referenced from:
_main in main-22f955.o
ld: symbol(s) not found for architecture x86_64
But if I include sum.cpp in my main program, then program will work as charm.

Undefined reference when using Cmake and QtCreator

I'm running the basic OpenCV example with OpenCV3.0.0 dev:
project(ImageDenoise)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
MESSAGE(${OpenCV_LIBS})
MESSAGE(${OpenCV_INCLUDE_DIRS})
Source code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char* argv[] )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], IMREAD_COLOR );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
When I import this project into QtCreator, I got the following linking errors when building project:
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
CMakeFiles/ImageDenoise.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x7c): undefined reference to `cv::imread(cv::String const&, int)'
main.cpp:(.text+0xf5): undefined reference to `cv::namedWindow(cv::String const&, int)'
main.cpp:(.text+0x144): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
However when I run cmake from command line and using make, then it works perfectly. What is the reason behind this?
dzung#Cronus:~/kSVD/build$ make
Scanning dependencies of target ImageDenoise
[100%] Building CXX object CMakeFiles/ImageDenoise.dir/main.cpp.o
Linking CXX executable ImageDenoise
[100%] Built target ImageDenoise
dzung#Cronus:~/kSVD/build$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake ImageDenoise Makefile
dzung#Cronus:~/kSVD/build$ ./ImageDenoise
usage: DisplayImage.out <Image_Path>
I had exactly the same issue. I think this is some sort of a Qt-creator bug.
I solved this by:
Remove everything except *.cpp and CMakeLists.txt in the project
folder.
Before creating anything with Qt-creator do: cmake . && make
Now open existing project in Qt-creator.
Now you can run cmake/compile/run etc just fine.