Page 1 of 1
Strange Problem using CMake (multiple definition error)
Posted: Mon Jul 18, 2016 8:53 pm
by Fox Mulder
Hi all,
today I tryed to build my Project with cMake in Codelite and I got strange results of it- so I created a new minimal project to test the behavier of CMake cause I never used it.
I have 3 files
main.cpp
Code: Select all
#include "test.h"
int main(int argc, char **argv)
{
return 0;
}
test.h
test.cpp
I run Cmake and build project but I get the compiler error, x is a multiple definition
What I am doing wrong here?
I use Cmake 3.6
Re: Strange Problem using CMake (multiple definition error)
Posted: Mon Jul 18, 2016 9:08 pm
by eranif
Please post the build log and all relevant information as described on this post:
http://codelite.org/forum/viewtopic.php?f=3&t=804
Thanks
Eran
Re: Strange Problem using CMake (multiple definition error)
Posted: Mon Jul 18, 2016 9:20 pm
by Fox Mulder
Build Output:
Code: Select all
C:\Windows\system32\cmd.exe /C cd C:\Users\Ghost\Documents\cmake-build-Debug && C:/WORKSPACE/TDM-GCC-64/bin/mingw32-make.exe -j4 SHELL=cmd.exe -e
[ 33%] Linking CXX executable bin\test111.exe
CMakeFiles\test111.dir/objects.a(test.cpp.obj):test.cpp:(.bss+0x0): multiple definition of `x'
CMakeFiles\test111.dir/objects.a(main.cpp.obj):main.cpp:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [bin/test111.exe] Error 1
CMakeFiles\test111.dir\build.make:123: recipe for target 'bin/test111.exe' failed
mingw32-make.exe[1]: *** [CMakeFiles/test111.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/test111.dir/all' failed
Makefile:82: recipe for target 'all' failed
====1 errors, 0 warnings====
Win7 64 bit
Cmake 3.6
Codelite 9.1.8
Compiler TDM-GCC-64
not self compiled Codelite
Maybe the CmakeLists.txt can help to find the problem.
Code: Select all
# -*- CMakeLists.txt generated by CodeLite IDE. Do not edit by hand -*-
cmake_minimum_required(VERSION 2.8.11)
# Workspace name
project(test11)
# This setting is useful for providing JSON file used by CodeLite for code completion
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
# Set default locations
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CONFIGURATION_NAME "Debug")
# Projects
# Top project
# Define some variables
set(PROJECT_PATH "${CMAKE_SOURCE_DIR}/")
#{{{{ User Code 1
# Place your code here
#}}}}
include_directories(
.
.
)
# Compiler options
add_definitions(-g)
add_definitions(-O0)
add_definitions(-Wall)
# Linker options
# Library path
set(CMAKE_LDFLAGS "${CMAKE_LDFLAGS} -L. ")
# Define the CXX sources
set ( CXX_SRCS
${CMAKE_SOURCE_DIR}/main.cpp
${CMAKE_SOURCE_DIR}/test.cpp
)
set_source_files_properties(
${CXX_SRCS} PROPERTIES COMPILE_FLAGS
" -g -O0 -Wall")
#{{{{ User Code 2
# Place your code here
#}}}}
add_executable(test111 ${CXX_SRCS} ${C_SRCS})
target_link_libraries(test111 ${LINK_OPTIONS})
#{{{{ User Code 3
# Place your code here
#}}}}
Re: Strange Problem using CMake (multiple definition error)
Posted: Mon Jul 18, 2016 10:13 pm
by eranif
Its not a CMake issue, its a bug in your code
Since you **define** the variable "int x;" in the header file "test.h" and then you include it **twice**
Once in test.cpp and later in main.cpp
This means that you have defined it twice (basically #include "test.h" is a compiler copy paste into the source file).
You need to define it once (in one of the .cpp files) and in the header file, add the "extern" keyword
A good answer (to a similar issue) is explained here:
http://stackoverflow.com/questions/1452 ... l-variable
Note: this is not a programming board, so please try to avoid asking such questions on this forum (unless its related to CodeLite)
Eran
Re: Strange Problem using CMake (multiple definition error)
Posted: Tue Jul 19, 2016 12:55 pm
by Fox Mulder
Oops, how I forgot this one. I should have know this.
The compiler output confused me a bit cause if you are using cMake and get this error and then swich to default build system, this error is gone.
So thats why I thought cMake needs something to rebuild or something like that.
If I create a new Project and start with the default build system then I get the same error message.
It was not clear for me anyway when I should run cMake from within Codelite, sometimes I get build error messages, then I run cMake and everything is fine again.
As I understand it now (and this is coming from many different pages where you have put some pieces together and hope you get the right picture, even you dont know how it should look like)
Cmake creates a make file and codelite builds using this make file. So in this case I need to rerun Cmake everytime I create a new file for the project. If this is the way how it works, codelite could maybe mark this state of file and run cMake automaticly if that make sense. But as I said I dont know how exactly this build works.
I should stick with writing batsh files for the build where I write down every step the compiler and linker is doing.
So sry for taking your time and thanks for help.
Re: Strange Problem using CMake (multiple definition error)
Posted: Tue Jul 19, 2016 2:39 pm
by eranif
Cmake creates a make file and codelite builds using this make file. So in this case I need to rerun Cmake everytime I create a new file for the project
Yes, this is how CMake works, you need to re-run cmake after adding new files
Fox Mulder wrote:codelite could maybe mark this state of file and run cMake automaticly if that make sense
Please open a feature request
Eran